Skip to content
Advertisement

Deprecating properties of class in python [closed]

I am trying to deprecate property of class.

class A:
   def __init__(self,
   variable1: int,
   ##to be deprecated
   variable2: int )
   {....}

Expected behaviour: If user tries to use variable 2 he should get warning that its deprecated.

Advertisement

Answer

You can implement variable2 as a property.

import warnings

class A:
    def __init__(self, variable1: int, variable2: int):
        self.variable1 = variable1
        self._variable2 = variable2

    @property
    def variable2(self):
        warnings.warn('The use of variable2 is deprecated.', DeprecationWarning)
        return self._variable2

    @variable2.setter
    def variable2(self, value: int):
        warnings.warn('The use of variable2 is deprecated.', DeprecationWarning)
        self._variable2 = value
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement