473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Overwriting property-> can't set attribute

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
Aug 22 '08 #1
6 14649
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
Aug 22 '08 #2
On Aug 22, 5:38*am, Gregor Horvath <g...@gregor-horvath.comwrote:
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
Aug 22 '08 #3
Raymond Hettinger a écrit :
On Aug 22, 5:38 am, Gregor Horvath <g...@gregor-horvath.comwrote:
>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 !-)

Aug 22 '08 #4
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
no******@hughes.net
Aug 22 '08 #5
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).
Aug 26 '08 #6
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.
Aug 26 '08 #7

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

Similar topics

0
by: Peter Royle | last post by:
Hello, I have a deployment project (VB.NET - I don't know whether the language matters) and I have a file in it which goes in the user's folder. This file gets modified by the application, so I...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
1
by: chris kittell | last post by:
Hi All, on db2 v8.1 for linux 32 bit redhat 7.3, when installing fixpak6,it's overwriting the symbolic link that I defined for the install directory, and proceeding to fill up my root partition....
9
by: Robert | last post by:
Hi, Is it me or is the recv() function not erasing but only overwriting the last data it supplied? example: when i first send: "login robert test"
19
by: tweak | last post by:
I have been messing around with buffers, and I found it peculiar that the code below will run without a segmentation fault. As far as I know, overwriting the allocated space from a call to...
9
by: jammie_linux | last post by:
Recently I came across a code which is quite similar to one shown below: #include <stdio.h> #include <string.h> typedef struct node { int data; int arr;
11
by: Steven Jones | last post by:
I have a C program that prints out two lines as follows: Line 1 Line 2 What I would like is for this program to sleep for one second, and then print out two more lines, overwriting the...
8
by: Yuk Tang | last post by:
I am tearing my hair out over this, since I can't see what I'm doing wrong (duh, if I knew, I wouldn't be asking the question). I am adding Field items to a Field Collection, but for some reason...
2
by: Nemisis | last post by:
Hi everyone, I have 2 pages, PageA and PageB. On PageA i allow our users to update a details of a record using textboxes, dropdownlists etc. When a user clicks on a button on PageA, i would...
27
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.