Property error

K

king kikapu

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

Thanks a lot in advance.
 
D

Dennis Lee Bieber

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.
What version of Python? Most recent versions don't need the
@property
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
.... _age = 0
.... def _getAge(self):
.... return self._age
.... def _setAge(self, value):
.... self._age = value
.... age = property(_getAge, _setAge)
....
Also: note that the actual age STORAGE is not "age" but "_age". You
might have been conflicting:

age = 0

def age(): replaces prior "age"
....
self.age = value reference the age(), not age
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

king kikapu

What version of Python? Most recent versions don't need the

Hi, thanks for the help!

I am using 2.5 version and i think i like more the @property decorator
instead of the property(...) syntax. Is the code changing much using
@property ??

Thanks again!
 
K

king kikapu

Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,
 
P

Peter Otten

king said:
Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

@decorator
def f():
# ...

is the same as

def f():
# ...
f = decorator(f())

What happens when your age() function is invoked? There is no explicit
return statement, so None is implicitly returned, and

age = property(age())

is the same as age = property(None)

i. e. you get a property with None as the getter. What you want is
.... return property(**f())
........ _age = "unknown"
.... @star_property
.... def age():
.... def fget(self):
.... return self._age
.... def fset(self, value):
.... self._age = value
.... return locals()
....42

However, that is rather hackish, and I recommend that you stick with the
standard approach given by Dennis and limit the use of property as a
decorator to read-only attributes:
.... @property
.... def age(self): return 42
....42

Peter
 
R

Rob Williscroft

king kikapu wrote in 16g2000cwy.googlegroups.com in comp.lang.python:
I am sure it is something very obvious

Yes, property is *NOT* a decorator, it can only be used as a decorator in
the one case that is mentioned in the docs:

<quote href="http://docs.python.org/lib/built-in-funcs.html#l2h-57">

If given, doc will be the docstring of the property attribute. Otherwise,
the property will copy fget's docstring (if it exists). This makes it
possible to create read-only properties easily using property() as a
decorator:

class Parrot(object):
def __init__(self):
self._voltage = 100000

@property
def voltage(self):
"""Get the current voltage."""
return self._voltage

</quote>

Note the use of "read-only" in the above description and that the
decorated function (voltage) *is* the properties "fget" function.

Rob.
 
P

Peter Otten

Peter Otten wrote:
@decorator
def f():
# ...

is the same as

def f():
# ...
f = decorator(f())

What happens when your age() function is invoked? There is no explicit
return statement, so None is implicitly returned, and

age = property(age())

is the same as age = property(None)

As Georg pointed out, this is all wrong. As age is not called the age
function becomes the getter.

Peter
 
G

Georg Brandl

king said:
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
 
G

George Sakkis

king said:
Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

As others have already replied, the default property doesn't work this
way. In your example, it is possible to write it in one line using
lambda:

class Person(object):

age = property(fget=lambda self: self._age,
fset=lambda self, value: setattr(self,'_age',value))

If any of fget,fset,fdel cannot be written as lambda or you'd rather
write them as functions, you may use the recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698.

HTH,
George
 
G

George Sakkis

king said:
Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

As others have already replied, the default property doesn't work this
way. In your example, it is possible to write it in one line using
lambda:

class Person(object):

age = property(fget=lambda self: self._age,
fset=lambda self, value: setattr(self,'_age',value))

If any of fget,fset,fdel cannot be written as lambda or you'd rather
write them as functions, you may use the recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698.

HTH,
George
 
G

George Sakkis

king said:
Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

As others have already replied, the default property doesn't work this
way. In your example, it is possible to write it in one line using
lambda:

class Person(object):

age = property(fget=lambda self: self._age,
fset=lambda self, value: setattr(self,'_age',value))

If any of fget,fset,fdel cannot be written as lambda or you'd rather
write them as functions, you may use the recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698.

HTH,
George
 
G

George Sakkis

king said:
Your example Dennis, work as expected. I understand the mistake i have
made. But when i try to fix the original code usihn @property now, it
gives me the same error.
So, here it 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


I am sure it is something very obvious but my skills does not (yet)
enable me to figure this out,,,

As others have already replied, the default property doesn't work this
way. In your example, it is possible to write it in one line using
lambda:

class Person(object):

age = property(fget=lambda self: self._age,
fset=lambda self, value: setattr(self,'_age',value))

If any of fget,fset,fdel cannot be written as lambda or you'd rather
write them as functions, you may use the recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698.

HTH,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top