Property error

Georg Brandl g.brandl-nospam at gmx.net
Fri Dec 15 07:50:19 EST 2006


king kikapu wrote:
> Hi to all,
> 
> i am trying to use properties in Python and i am sure i have made
> something wrong with the below code but i just cannot see what it is.
> 
> Can anyone please help me on this ?
> 
> The code is :
> 
> class Person(object):
>     age = 0
> 
>     @property
>     def age():
>         def fget(self):
>             return self.age
>         def fset(self, value):
>             self.age = value
> 
> me = Person()
> me.age = "34"
> print me.age
> 
> 
> and i am getting the error:
> 
> "  File "C:\projects\Python\p2.py", line 12, in <module>
>     me.age = "34"
> AttributeError: can't set attribute "
> 
> What exactly i am doing wrong ??

This will not work. There are some versions of a property decorator
flowing around that allow you to do a similar thing, like

class Person(object):
     age = 0

     @Property
     def age():
         def fget(self):
             return self.age
         def fset(self, value):
             self.age = value
         return locals()

but here, Property is not the built-in "property" function.

It is impossible to use the built-in property function as a decorator to create
a property that isn't read-only.

Georg



More information about the Python-list mailing list