> -----Original Message----- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of wswilson > Sent: Wednesday, April 11, 2007 9:24 AM > To: python-list@python.org > Subject: passing class by reference does not work?? > > Here is my code: > > class A(): > val = 0 > > def b(item, a): > a.val = a.val + 1 > return item + a.val > > def c(): > d = [1, 2, 3] > print [b(item, A()) for item in d] > > c() > > I expected this to output [2, 4, 6]. However, it outputs [2, 3, 4] > which is not what I wanted. I thought that if I passed the A() > instance in my list comprehension in c(), then the changes I made to > a.val in b() would be reflected in the A() instance next time the list > comprehension called b(). But, obviously that is not happening. I'm > kinda new at python so I may be missing something obvious here. > > Any suggestions?
A() is not the class A. It calls the constructor of class A, returning an instance. If you change that line to: print [b(item, A) for item in d] you'll get the output you expected. --- -Bill Hamilton [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list