I was looking around and ran across the python builtin function property(). In other programming languages we are used to making things public/private and using getters/setters. In python this is not the usual way of doing things, you just set an attribute directly. We also try to use underscores to denote that an attribute should not be changed outside the class. Since people are not used to calling getters/setters in python they came up with the property function.
Say I have a class X with a single attribute y. I don’t want anyone to modify the y attribute without calling a setter function, well what I can do is something like the following
class X:
def __init__(self):
self._y = 1
def get_y(self):
return self._y
def set_y(self, value):
self._y = value
y = property(get_y, set_y)
Now when anyone does something like
temp = X()
temp.y = 5
it does not set y directly but invisibly calls the setter, likewise
print temp.y
will call the getter invisibly.
You can have a look at more documentation here: http://docs.python.org/lib/built-in-funcs.html There is also a good blog post here http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/
Another cool thing is that you can create a read-only property out of a function, this would be like a constant, which is kind of cool.