property

property([fget[, fset[, fdel[, doc]]]]) => descriptor

Returns a property descriptor 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 deleting, an attribute. Typical use is to define a managed attribute x:

class C(object):
    def __init__(self): self.__x = None
    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.")

o = C()
value = o.x # calls getx
o.x = value # calls setx
del o.x # calls delx

New in version 2.2.

 

Comment:

could mention the @property shortcut for readonly attributes here: class C(object): @property def x(self): return value turns the "x" method into a "getter" for a read-only attribute with the same name. Note that this syntax also works for old-style classes, but the resulting attribute isn't read-only. If you assign to the attribute, you'll overwrite the property descriptor.

Posted by infogami

A Django site. this page was rendered by a django application in 0.04s 2008-12-03 20:20:41.155364. hosted by webfaction.