passing class by reference does not work??
wswilson
wswilson at gmail.com
Wed Apr 11 10:42:14 EDT 2007
On Apr 11, 10:36 am, "antred" <Nut... at gmx.net> wrote:
> > 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.
OK, I see that. I thought I was creating an instance of the class A()
when I called the list comprehension and that the a parameter in b()
was then a reference to that instance. What can I do instead?
More information about the Python-list
mailing list