passing class by reference does not work??

antred NutJob at gmx.net
Wed Apr 11 10:36:50 EDT 2007


> def b(item, a):
>         a.val = a.val + 1
>         return item + a.val




This is where the problem lies, specifically the line a.val = a.val +
1
What happens here is that the 1st a.val refers to a member of the
class instance a, called val ... which does not yet exist and is
therefore created as the result of taking the val member of the class
A and adding 1 to it. In other words, a.val is not the same variable
as A.val. Are you following? If not, change your b() method to this:

def b(item, a):
        a.val = a.val + 1
        assert a.val is A.val
        return item + a.val

and see what happens.




More information about the Python-list mailing list