Python Setter and Getter :
Without Setter and getter , in the below example when the users first
name or last name is changed individually his full name is not getting
changed :
Method 1 : Without Decorator
_value=0
def __init__(self,value):
self._value = value
def get_value(self):
return self._value
def set_value(self, x):
self._value = x
---------------------------------------------------------------------------------
Method 2 : With Decorator
class a():_value=0
def __init__(self,value):
self._value = value
@property
def value(self):
return self._value
@value.setter # should match the method name of @property
def value(self, a):
self._value = a
A=a("hi")
A.value=10
print(A.value)
No comments:
Post a Comment