473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Apr 11 '07 #1
4 1525
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.

Apr 11 '07 #2
wswilson <ws******@gmail.comwrote:
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.
Yep:

a.val = a.val + 1

sets in INSTANCE variable a the value computed on the RHS. A.val (the
CLASS variable) is never changed. You're not "passing the class", of
course, but rather an instance of the class.

To increment A.val, you need to assign to the class variable, or write
some method in class A which assigns to the class variable. If you
want, you can change class A only, leaving all of the rest of your code
untouched, with a property (but then A needs to be newstile), e.g.:

class A(object):
_val = 0
def getval(self): return A._val
def setval(self, value): A._val = value
val = property(getval, setval)

now your code should work as intended: any read access to a.val returns
A._val, and any setting of a.val actually sets A._val, as you appear to
desire.
Alex
Apr 11 '07 #3
On Apr 11, 10:36 am, "antred" <Nut...@gmx.netwrote:
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?

Apr 11 '07 #4
On Apr 11, 10:40 am, a...@mac.com (Alex Martelli) wrote:
wswilson <wswil...@gmail.comwrote:
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.

Yep:

a.val = a.val + 1

sets in INSTANCE variable a the value computed on the RHS. A.val (the
CLASS variable) is never changed. You're not "passing the class", of
course, but rather an instance of the class.

To increment A.val, you need to assign to the class variable, or write
some method in class A which assigns to the class variable. If you
want, you can change class A only, leaving all of the rest of your code
untouched, with a property (but then A needs to be newstile), e.g.:

class A(object):
_val = 0
def getval(self): return A._val
def setval(self, value): A._val = value
val = property(getval, setval)

now your code should work as intended: any read access to a.val returns
A._val, and any setting of a.val actually sets A._val, as you appear to
desire.

Alex
Thanks, that should work well. I appreciate it.

Apr 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: John C | last post by:
Hi, I am a little uncertain about the concept of passing a reference to a class to another instance of a class. for instance I thought that the following was ok: Network network = Network();...
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.