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 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
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
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 !-)
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
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).
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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....
|
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"
|
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...
|
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;
|
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...
|
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...
|
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...
|
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?
...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
| |