Overwriting property-> can't set attribute

G

Gregor Horvath

Hi,

why is this code failing?

class B(object):
pass

B.testattr = property(lambda s:"hallo")
b = B()
b.testattr = "test"


Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

/tmp/python-14202ViU.py in <module>()
14 B.testattr = property(lambda s:"hallo")
15 b = B()
---> 16 b.testattr = "test"
17
18

<type 'exceptions.AttributeError'>: can't set attribute
 
G

Gregor Horvath

Gregor said:
why is this code failing?

OK I answer myself :)

Because there is not fset function definied in the property.
I have to del the attr before rebinding the attributename to another object.
 
R

Raymond Hettinger

why is this code failing?

class B(object):
     pass

B.testattr = property(lambda s:"hallo")
b = B()
b.testattr = "test"

First, property() only works when attached to classes, not instances.
So the assignment should be: B.testattr = property(...)

Second, the property() call in you example only defines a getter
function (the first argument) and not a setter function (the second
argument) it defaults to a read-only property. That is why
b.testattr='test' would still fail.

Raymond
 
B

Bruno Desthuilliers

Raymond Hettinger a écrit :
First, property() only works when attached to classes, not instances.
So the assignment should be: B.testattr = property(...)

Mmm... You may want to reread the OP code more carefully !-)
 
N

norseman

Gregor said:
Hi,

why is this code failing?

class B(object):
pass

B.testattr = property(lambda s:"hallo")
b = B()
b.testattr = "test"


Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

/tmp/python-14202ViU.py in <module>()
14 B.testattr = property(lambda s:"hallo")
15 b = B()
---> 16 b.testattr = "test"
17
18

<type 'exceptions.AttributeError'>: can't set attribute
====================================

b = B() # synonyms
When B.testattr = "hallo" so does b.testattr
So if in subsequent code:
B.testattr = property(lambda s:"test")
Then:
b.testattr yields "test"
unless b was essentially destroyed/reused in between times


this is how/why things like:
gc = damn-lot-of-typing-due-to-long-names
gc.something
works as if it was:
damn-lot-of-typing-due-to-long-names.something


Steve
(e-mail address removed)
 
B

Bruno Desthuilliers

norseman a écrit :
====================================

b = B() # synonyms

Not exactly, no. You probably missed the call operator applied to B.

(snip erroneous explanation).
 
B

Bruno Desthuilliers

Gregor Horvath a écrit :
Hi,

why is this code failing?

class B(object):
pass

B.testattr = property(lambda s:"hallo")
b = B()
b.testattr = "test"


Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

/tmp/python-14202ViU.py in <module>()
14 B.testattr = property(lambda s:"hallo")
15 b = B()
---> 16 b.testattr = "test"
17
18

<type 'exceptions.AttributeError'>: can't set attribute

It's not failing, it's doing exactly what's expected. You made testattr
a read-only property by not providing a setter.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top