setting an attribute

7

7stud

"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.
 
B

Bruno Desthuilliers

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.
 
H

half.italian

"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...
.... def __init__(self):
.... self.x[0] =7
....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
 
7

7stud

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top