470,821 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Property In Python

I want to know how we can write Properties in Pyhton.Any one knows
good doc for this one?

Apr 21 '06 #1
3 1403
class person(object):
def _get_age(self):
return self.__age
age = property(_get_age) # a read-only property

def _get_name(self):
return self.__name
def _set_name(self, value):
self.__name = value
name = property(_get_name, _set_name)

Apr 21 '06 #2
>>> print property.__doc__
property(fget=None, fset=None, fdel=None, doc=None) -> property

fget is a function to be used for getting an attribute value, and
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.")

Apr 21 '06 #3
I started with the "How-To Guide for Descriptors" by Raymond Hettinger

It is one of several docs on the "New-style Classes" page at python.org

Apr 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Guido van Rossum | last post: by
3 posts views Thread by Rebecca Taylor | last post: by
3 posts views Thread by Jules Dubois | last post: by
11 posts views Thread by Laszlo Zsolt Nagy | last post: by
25 posts views Thread by Michal Kwiatkowski | last post: by
3 posts views Thread by bearophileHUGS | last post: by
18 posts views Thread by David Moss | last post: by
8 posts views Thread by Hussein B | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.