Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Overwriting property-> can't set attribute

Reply
Thread Tools

Overwriting property-> can't set attribute

 
 
Gregor Horvath
Guest
Posts: n/a
 
      08-22-2008
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
 
Reply With Quote
 
 
 
 
Gregor Horvath
Guest
Posts: n/a
 
      08-22-2008
Gregor Horvath schrieb:
>
> 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
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      08-22-2008
On Aug 22, 5:38*am, Gregor Horvath <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-22-2008
Raymond Hettinger a écrit :
> On Aug 22, 5:38 am, Gregor Horvath <(E-Mail Removed)> wrote:
>> 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 !-)

 
Reply With Quote
 
norseman
Guest
Posts: n/a
 
      08-22-2008
Gregor Horvath wrote:
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-26-2008
norseman a écrit :
> Gregor Horvath wrote:
>> 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).
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-26-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
..HtmlControls.HtmlInputFile.Saveas(filename) not overwriting =?Utf-8?B?UmVkZHk=?= ASP .Net 5 12-28-2005 07:58 PM
How do you write to text files WITHOUT OVERWRITING? javajavalink Java 6 12-14-2004 06:28 PM
overwriting fields from abstract class Peter Grison Java 3 04-27-2004 05:14 PM
Java concurrent transactions overwriting Dave Ficken Java 1 11-10-2003 08:25 PM
AXIS and Java - concurrent transactions overwriting each other Dave Ficken Java 0 11-08-2003 02:19 PM



Advertisments