473,471 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Property error

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.

Dec 14 '06 #1
11 1249
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!

Dec 15 '06 #2

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

Dec 15 '06 #3
king kikapu wrote:
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
>>def star_property(f):
.... return property(**f())
....
>>class Person(object):
.... _age = "unknown"
.... @star_property
.... def age():
.... def fget(self):
.... return self._age
.... def fset(self, value):
.... self._age = value
.... return locals()
....
>>person = Person()
person.age
'unknown'
>>person.age = 42
person.age
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:
>>class Forever42(object):
.... @property
.... def age(self): return 42
....
>>Forever42().age
42

Peter

Dec 15 '06 #4
Peter Otten wrote:
@decorator
def f():
# ...

is the same as

def f():
# ...
f = decorator(f())
^^
Nope, f is not called here. (Think of staticmethod).

Georg
Dec 15 '06 #5
king kikapu wrote in news:1166181267.949316.197360@
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.
--
http://www.victim-prime.dsl.pipex.com/
Dec 15 '06 #6
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
Dec 15 '06 #7
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

Dec 15 '06 #8
king kikapu wrote:
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/Coo.../Recipe/410698.

HTH,
George

Dec 15 '06 #9
king kikapu wrote:
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/Coo.../Recipe/410698.

HTH,
George

Dec 15 '06 #10
king kikapu wrote:
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/Coo.../Recipe/410698.

HTH,
George

Dec 15 '06 #11
king kikapu wrote:
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/Coo.../Recipe/410698.

HTH,
George

Dec 15 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JBeerhalter | last post by:
Has anyone ever seen a case where the a TreeNode's color property seems to be ignored? I have the following line of code newNode.ForeColor = Color.Red; But now matter where I stick this...
0
by: scpedicini | last post by:
Okay, I've been having a problem with custom design-time properties that has been driving me nuts for about a week. The control that I'm working on extends System.Windows.Forms.AxHost, but I'm...
4
by: TW Scannell | last post by:
Hello, When I try to change the Anchor property of a textbox, from "Top, Left" to all four sides I get an "Invalid Property Value" Error. The details state "Cannot widen from target type to...
9
by: Taishi | last post by:
ExecuteReader: Connection property has not been initialized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
3
by: jack bretcher | last post by:
I am developing a DDL that will be a wrapper for an existing set of libs. What I am trying to do is implement the class.error property in my wrapper. I have tried to create a readonly property...
6
by: ljstern | last post by:
Hello. I am using MS Access 2003 with XP/pro. I have a form that has the following properties set through the property sheet: form: AllowAdditions = False CartridgeID field: Locked = True ...
2
by: MitchellEr | last post by:
Hi, I've seen various forum posts that mention the ability to set ASPSmaryUpload's Codepage property in order to handle UTF-8 and other character encodings. Example:...
0
acoder
by: acoder | last post by:
Problem When setting the FORM object's action property an "Object does not support this property or method" error occurs Browser Internet Explorer 6- Example The Javascript code: var...
0
by: Kurt Pindroh | last post by:
Connected to ibm DB2 database the following php code: $stmt = $dbh->query('select trim(FPDESC) as FPDESC from mylib.myfile'); $obj = $stmt->fetch(PDO::FETCH_OBJ); var_dump($obj); echo '<br>';...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.