An interesting python problem

J

Johnny Lee

Hi,
Look at the follow command in python command line, See what's
interesting?:)
1 0

---------------------------------------
class A: arr = []
a = A()
b = A()
a
a.arr.append("haha")
print a.arr , b.arr ['haha'] ['haha']
a.arr = ["xixi"]
print a.arr , b.arr ['xixi'] ['haha']
A.arr ['haha']
A.arr.append("xx")
A.arr ['haha', 'xx']
a.arr ['xixi']
b.arr ['haha', 'xx']
b.arr.pop() 'xx'
b.arr ['haha']
A.arr
['haha']
[/QUOTE]
def __init__(self):
self.arr = []['haha'] []
 
B

bruno modulix

Johnny said:
Hi,
Look at the follow command in python command line, See what's
interesting?:)



i = 0


1 0

Quite what I would expect. First you declare i as being a *class*
attribute of A, with value 0. Then you create 2 instances a and b of A.
Then you add to a an *instance* variable named i (that then shadows the
class variable of the same name), with value 1. Then you print a.i,
wihci is the instance variable i of a, and b.i, which is the class
variable i of A, with value 0.
---------------------------------------


arr = []

['haha'] ['haha']

Now you create a class A with a *class* variable arr which is an empty
list, and 2 instances a and b of A. Then you append to a.arr - which is
A.arr, so when you print a.arr and b.arr, you in fact print A.arr

Then you add an instance variable arr to a, shadowing A.arr
['xixi'] ['haha']

So now you print a.arr and A.arr (accessed thru b)

(snip)

def __init__(self):
self.arr = []

['haha'] []

Here you define a class X with an *instance* variable arr, and two
instances m and n of X, then append to m.arr, which of course has no
impact on n.

I dont see anything interesting nor problematic here. If you understand
the difference between class attributes and instance attributes, the
difference between mutating an object and rebinding a name, and the
attribute lookup rules in Python, you'll find that all this is the
normal and expected behavior.

Or did I miss something ?
 
J

Johnny Lee

bruno said:
I dont see anything interesting nor problematic here. If you understand
the difference between class attributes and instance attributes, the
difference between mutating an object and rebinding a name, and the
attribute lookup rules in Python, you'll find that all this is the
normal and expected behavior.

Or did I miss something ?

No, you didn't miss anything as I can see. Thanks for your help:)
 
B

bruno modulix

Johnny said:
No, you didn't miss anything as I can see. Thanks for your help:)

You're welcome !-)

Ok, I guess all this is not that intuitive, specially when comes from
less dynamic languages. Python's object model is quite powerful, but one
need to have a good understanding of it to understand *why* it works
that way. There's an interesting slideshow about metaclasses and
descriptors that may help (if your brain is robust enough !-) :

http://www.python.org/pycon/dc2004/papers/24/metaclasses-pycon.pdf

HTH
 

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

Forum statistics

Threads
474,264
Messages
2,571,317
Members
48,003
Latest member
coldDuece

Latest Threads

Top