473,320 Members | 1,921 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,320 software developers and data experts.

setting an attribute

"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special, yet it doesn't affect any __dict__
entry for the attribute in the instance--instead it is affecting a
__dict__ entry for the attribute in the class.

May 16 '07 #1
3 1550
7stud a écrit :
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special,

self.x[0] = 10 doesn't bind self.x - it's just syntactic sugar for
self.x.__setitem__(0, 10) (which itself is syntactic sugar for
list.__setitem__(self.x, 0, 10))
yet it doesn't affect any __dict__
entry for the attribute in the instance
Of course. The name 'x' is looked up in the instance, then in the class.
Since there's no binding (only a method call on a class attribute),
instance's dict is not affected.

May 16 '07 #2
On May 16, 12:34 am, 7stud <bbxx789_0...@yahoo.comwrote:
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special, yet it doesn't affect any __dict__
entry for the attribute in the instance--instead it is affecting a
__dict__ entry for the attribute in the class.
I think it's following scope rules. It can't find an attribute for
self.x in the instance. It then checks the class for the var and
finds it and sets it there. It would error otherwise...
>>class Test(object):
.... def __init__(self):
.... self.x[0] =7
....
>>t = Test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
AttributeError: 'Test' object has no attribute 'x'

~Sean

May 16 '07 #3
On May 16, 2:24 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
7stud a écrit :
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."
In light of that statement, how would one explain the output of this
code:
class Test(object):
x = [1, 2]
def __init__(self):
self.x[0] = 10
print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}
It looks to me like self.x[0] is binding on an instance whose
attribute name is not special,

self.x[0] = 10 doesn't bind self.x - it's just syntactic sugar for
self.x.__setitem__(0, 10) (which itself is syntactic sugar for
list.__setitem__(self.x, 0, 10))
yet it doesn't affect any __dict__
entry for the attribute in the instance

Of course. The name 'x' is looked up in the instance, then in the class.
Since there's no binding (only a method call on a class attribute),
instance's dict is not affected.
Thanks.

May 16 '07 #4

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

Similar topics

4
by: G. Richard Bellamy | last post by:
I'm trying to unset the Encrypted attribute on all the files in a path. The attribute is not getting set. What am I doing wrong? Perhaps there's another newsgroup I can send this to? Here's...
3
by: AndyG | last post by:
I'm trying to mess about with a table using javascript. I'm deleting the current row then adding a new row and adding a couple of columns. I then want to add either a style attribute and add a...
0
by: Edward Diener | last post by:
Since a __property can be split between a read access and a write access property in MC++, how does one set an attribute on a __property ? Is setting an attribute before either the read access...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
0
by: Torsten Sturm | last post by:
Hello, SP1 of Windows Server introduced a new "feature" in the GlobalizationConfig class called EnableBestFitResponseEncoding. Effect is that when you set anything other than UTF8 in...
7
by: Jazz | last post by:
Hello, I am using VSNet 2003 and I am trying to programmatically detect if debug attribute in the compilation tag is true or false. How can I do this ? Thanks, Jazz
3
by: Valvalis | last post by:
Hello, I am trying to set a class attribute of a text.item element to the value of its nearest ancestor. I want to do this in the case that the class of the text.item is currently a blank string....
5
by: tleeuwenburg | last post by:
I have an object and wish to set an attribute on it which, unfortunately for me, is read-only. How can I go about this? Cheers. -T
4
by: mk | last post by:
Hello everyone, I try to set two properties, "value" and "square" in the following code, and arrange it in such way that setting one property also sets another one and vice versa. But the code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.