[Tutor] Variable Modification in a class

Alan Trautman ATrautman@perryjudds.com
Tue Jun 3 12:22:01 2003


Let's try this:
You can see that the assignment doesn't copy the values is assigns them by
pointing to the same memory address.

Try running this:

class AB:
    def __init__(self):
        self.a = None
        self.b = None

def func(ab):
    b = ab()
    c = ab()
    print "init c", c #memory locations prior to assignment
    print "init b", b
    print "these are different"
    
    b.a = 5
    b.b = 10

    c = b   # This block assigns c the same memory location as b
            # This means any value placed in one will be in the other
            # You have effectively lost b and any values that were
            # assigned there
    print "final c",c 
    print "final b", b
    c.a = 30
    c.b = 40

    print b.a, b.b
    print c.a, c.b
   
t = func(AB)


HTH,
Alan