What is the difference between @property, @property.setter and getattr(), setattr(). When will I use them? If both are the same, which one is preferred to use in python 3.x?
I am new to python and not understanding where to use and how to use them with OO python
I have gone through many websites, but I'm not sure which one to use. Please give me some real time example. Appreciate your help.
EX:
getattr()
class emp: name='Harsh' salary='25000' def show(self): print self.name print self.salary e1 = emp() print getattr(e1,'name') setattr(e1,'height',152) @ property
class P:
def __init__(self,x):
self.x = x
@property
def x(self):
return self.__x
@x.setter
def x(self, x):
if x < 0:
self.__x = 0
elif x > 1000:
self.__x = 1000
else:
self.__x = x