468,288 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

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 1392
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by G. Richard Bellamy | last post: by
3 posts views Thread by AndyG | last post: by
reply views Thread by Edward Diener | last post: by
3 posts views Thread by Patrick | last post: by
7 posts views Thread by Jazz | last post: by
3 posts views Thread by Valvalis | last post: by
5 posts views Thread by tleeuwenburg | last post: by
4 posts views Thread by mk | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.