Fire event when variable is Set/Get

tharaka tharakawick at gmail.com
Mon Jul 25 00:06:07 EDT 2005


You are in luck because Python has "Properties" just like .NET.

For details lookup the documentation of the built-in function
property(). I'll just paste it here:


property( [fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive
from object).
fget is a function for getting an attribute value, likewise fset is a
function for setting, and fdel a function for del'ing, an attribute.
Typical use is to define a managed attribute x:


class C(object):
    def getx(self): return self.__x
    def setx(self, value): self.__x = value
    def delx(self): del self.__x
    x = property(getx, setx, delx, "I'm the 'x' property.")


... now u can use x like any variable and, python will get & set it
through the appropriate methods. Hope this answers your question.




More information about the Python-list mailing list