@property in python.
To understand i found some links to make it clear.
1. http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters
2. http://stackoverflow.com/questions/6304040/real-world-example-about-how-to-use-property-feature-in-python
First and last answer here are important.
3. The article for this topic here.
http://eli.thegreenplace.net/2009/02/06/getters-and-setters-in-python/
4. difference in
4.1 class class_name():
pass
4.2 class class_name(object):
pass
Note: one important use of this : (Not changing variables).
Try the above code and feel the power of property.
1. http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters
2. http://stackoverflow.com/questions/6304040/real-world-example-about-how-to-use-property-feature-in-python
First and last answer here are important.
3. The article for this topic here.
http://eli.thegreenplace.net/2009/02/06/getters-and-setters-in-python/
4. difference in
4.1 class class_name():
pass
4.2 class class_name(object):
pass
Note: one important use of this : (Not changing variables).
One simple use case will be to set a read only instance attribute , as you know leading a variable name with one underscore
_x
in python usually mean it's private (internal use) but sometimes we want to be able to read the instance attribute and not to write it so we can use property
for this:>>> class C(object):
def __init__(self, x):
self._x = x
@property
def x(self):
return self._x
>>> c = C(1)
>>> c.x
1
>>> c.x = 2
AttributeError Traceback (most recent call last)
AttributeError: can't set attribute
Note:Try the above code and feel the power of property.