[Tutor] properties not working in 2.2

Andy W toodles@yifan.net
Tue, 18 Dec 2001 16:39:40 +0800


> but what is property - and what is it for?
>
> Gregor

Quoting directly from: http://www.python.org/2.2/descrintro.html#property

"Properties are a neat way to implement attributes whose usage resembles
attribute access, but whose implementation uses method calls. These are
sometimes known as "managed attributes". In prior Python versions, you could
only do this by overriding __getattr__ and __setattr__; but overriding
__setattr__ slows down all attribute assignments considerably, and
overriding __getattr__ is always a bit tricky to get right. Properties let
you do this painlessly, without having to override __getattr__ or
__setattr__."

HTH,
Andrew

>
> ----- Original Message -----
> From: "karthik Guru" <karthikg@aztec.soft.net>
> To: <tutor@python.org>
> Sent: Tuesday, December 18, 2001 6:09 AM
> Subject: [Tutor] properties not working in 2.2
>
>
> > hi all,
> >
> > http://www.python.org/2.2/descrintro.html#property
> >
> > there is an example to use properties which is not workign for me under
> > Python 2.2a1
> >
> > class C(object):
> >     def __init__(self):
> >         print "hello world"
> >         self.__x = 0
> >     def getx(self):
> >         print "get called"
> >         return self.__x
> >     def setx(self, x):
> >         print "set called"
> >         if x < 0: x = 0
> >         self.__x = x
> >     x = property(getx,setx,None,"this is property")
> >
> >
> > this is the trace....
> >
> >   File "test.py", line 13, in C
> >     x = property(getx,setx,None,"this is property")
> > NameError: name 'property' is not defined
> >
> > can someone tell me as what's wrong here? ..python2.2 a1 does not have
> this
> > feature??
> >
> > thanx,
> > karthik
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>