[Tutor] properties not working in 2.2

karthik Guru karthikg@aztec.soft.net
Tue, 18 Dec 2001 14:48:09 +0530


class test:
	def __init__(self):
		pass
	__getattr__(self,name):


t = test()
t.age = 100
print t.age

the link has a detailed explanation for properties.
some of things i found very important..

__getattr__ is only called if age isn't found in the instance's dictionary.
so in this case it won't be called. __setattr__ is always called when we set
a value to an attribute.

but in case of properties, we have the new keyword "property" with which we
can qualify
an attribute as a property.

we can define accessor and mutator methods for that and register the same
using the
property keyword. Both the special methods "will" be called whenever someone
tries to change / access it.

so indirectly, we know when someone reads / sets an attribute and we can put
our custom code there. for eg: You might want to reject some values in your
set by
throwing an exception. Or you might not want a particular to client to read
a property ..
(so that code goes into the corresponding "get").

There was just one __getattr__ available to handle all attributes earlier.
But now we can have separate get and set for different atributes.

Also, if you don't define a set for a property named x, then it
automatically becomes read only.
if someone tries to set the value to that attribute it raises an
AttributeError.

I have observed such a feature in MS's C# as well. This was there in C#
specs long back.
Can some one tell from their experience if it's present in other languages
as well OR is it a MS innovation :-)

please refer to this for a very good explanation.
http://www.python.org/2.2/descrintro.html#property

karthik.














-----Original Message-----
From: Gregor Lingl [mailto:glingl@aon.at]
Sent: Tuesday, December 18, 2001 11:59 AM
To: karthik Guru; tutor@python.org
Subject: Re: [Tutor] properties not working in 2.2


My installation of Python 2.2c1 definitely
doesn't show this (or any other) error-massage
when running your example

instead, I got:

>>> huch = C()
hello world
>>> huch.x
get called
0
>>>

but what is property - and what is it for?

Gregor

----- 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
>