Connecting Tech Pros Worldwide Forums | Help | Site Map

Overwriting property-> can't set attribute

Gregor Horvath
Guest
 
Posts: n/a
#1: Aug 22 '08
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

--
Greg

Gregor Horvath
Guest
 
Posts: n/a
#2: Aug 22 '08

re: Overwriting property-> can't set attribute


Gregor Horvath schrieb:
Quote:
>
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.

--
Greg
Raymond Hettinger
Guest
 
Posts: n/a
#3: Aug 22 '08

re: Overwriting property-> can't set attribute


On Aug 22, 5:38*am, Gregor Horvath <g...@gregor-horvath.comwrote:
Quote:
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
Bruno Desthuilliers
Guest
 
Posts: n/a
#4: Aug 22 '08

re: Overwriting property-> can't set attribute


Raymond Hettinger a écrit :
Quote:
On Aug 22, 5:38 am, Gregor Horvath <g...@gregor-horvath.comwrote:
Quote:
>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(...)
Mmm... You may want to reread the OP code more carefully !-)

norseman
Guest
 
Posts: n/a
#5: Aug 22 '08

re: Overwriting property-> can't set attribute


Gregor Horvath wrote:
Quote:
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
>
--
Greg
--
http://mail.python.org/mailman/listinfo/python-list
>
====================================

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
norseman@hughes.net
Bruno Desthuilliers
Guest
 
Posts: n/a
#6: Aug 26 '08

re: Overwriting property-> can't set attribute


norseman a écrit :
Quote:
Gregor Horvath wrote:
Quote:
>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
>>
>--
>Greg
>--
>http://mail.python.org/mailman/listinfo/python-list
>>
====================================
>
b = B() # synonyms
Not exactly, no. You probably missed the call operator applied to B.

(snip erroneous explanation).
Bruno Desthuilliers
Guest
 
Posts: n/a
#7: Aug 26 '08

re: Overwriting property-> can't set attribute


Gregor Horvath a écrit :
Quote:
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.
Closed Thread