An interesting python problem

Adriaan Renting renting at astron.nl
Wed Sep 14 06:38:25 EDT 2005


In my mind all Python variables are some kind of "named pointers", I find that thinking this way helps me a lot in understanding what I'm doing. I know that this is not completely technically correct as in the first two examples there is actually a new a.i/a.arr created that shadows A.i, but thinking like this helps me. Are there fundamental flaws to think this way?

Example:
>>>class A: 
i = 0      ## Class A has a pointer named i pointing to (int 0 object)
           ## A.i -> (int 0 object)
>>>a = A() ## point a.i to the same thing A.i points to
           ## A.i -> (int 0 object)
           ## a.i -> (int 0 object)
>>>b = A() ## point b.i to the same thing A.i points to
           ## A.i -> (int 0 object)
           ## a.i -> (int 0 object)
           ## b.i -> (int 0 object)
>>>a.i = 1 ## point a.i to a new (int object)
           ## A.i -> (int 0 object)
           ## b.i -> (int 0 object)
           ## a.i -> (int 1 object)
 
>>>class A: 
arr = [] ## A.i -> (empty list object)
>>>a = A()              ## point a.arr to the same thing A.arr points to
                        ## A.arr -> (empty list object)
                        ## a.arr -> (empty list object)
>>>a.arr.append("haha") ## insert ("haha" string object) into (empty list object) both a.i and A.i point to
                        ## A.i -> (list object) -> ("haha" string object)
                        ## a.i -> (list object) -> ("haha" string object)
>>>a.arr = ["xixi"]     ## point a.arr to a new (list object) pointing to a new ("xixi" string object)
                        ## A.i -> (list object) -> ("haha" string object)
                        ## a.i -> (different list object) -> ("xixi" string object)
>>>A.arr.append("xx")   ## insert ("xx" string object) into (list object) A.i points to
                        ## A.i -> (list object) -> ("haha" string object),("xx" string object)
                        ## a.i -> (different list object) -> ("xixi" string object)
etc. ...
 
------------------------------------- 
 
>>>class X: 
def __init__(self): 
self.arr = []           ## instances of Class X have a named pointer arr pointing to a new (empty list object)
                        ## X.arr does not exist!
>>>m = X()              ## creates a new (empty list object) and has m.arr point to it
                        ## m.arr -> (empty list object)
>>>n = X()              ## creates a new (empty list object) and has n.arr point to it
                        ## m.arr -> (empty list object)
                        ## n.arr -> (different empty list object)
>>>m.arr.append("haha") ## insert ("haha" string object) into (list object) m.i points to
                        ## m.i -> (list object) -> ("haha" string object)
                        ## n.i -> (different empty list object)

-- 
http://mail.python.org/mailman/listinfo/python-list 




More information about the Python-list mailing list