473,327 Members | 1,952 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,327 software developers and data experts.

Simple Class/Variable passing question

Hello,

I'm new to python, and PythonCard. In the code below, I'm trying to
create a member variable (self.currValue) of the class, then just pass
it to a simple function (MainOutputRoutine) to increment it. I thought
Python "passed by reference" all variables, but the member variable
doesn't seem to be incremented. If I make the function just increment
the member variable directly, without passing it in, it works fine?

In the code below, "self.currValue" stays at 5, and value evaluates to
1? Any insight would be appreciated...
class TestModel(model.Background):

def on_initialize(self,event):
self.currValue = 5

def on_startBtn_mouseClick(self, event):
self.MainOutputRoutine(self.currValue)
self.OutputRoutine(self.currValue)

def OutputRoutine(self,value):
self.components.txtBox1.text = str(value)

def MainOutputRoutine(self,value):
value = value + 1
Jun 27 '08 #1
2 1403
monkeyboy wrote:
Hello,

I'm new to python, and PythonCard. In the code below, I'm trying to
create a member variable (self.currValue) of the class, then just pass
it to a simple function (MainOutputRoutine) to increment it. I thought
Python "passed by reference" all variables, but the member variable
doesn't seem to be incremented. If I make the function just increment
the member variable directly, without passing it in, it works fine?

In the code below, "self.currValue" stays at 5, and value evaluates to
1? Any insight would be appreciated...
class TestModel(model.Background):

def on_initialize(self,event):
self.currValue = 5

def on_startBtn_mouseClick(self, event):
self.MainOutputRoutine(self.currValue)
self.OutputRoutine(self.currValue)

def OutputRoutine(self,value):
self.components.txtBox1.text = str(value)

def MainOutputRoutine(self,value):
value = value + 1
That's not how Python works. When you call
"self.MainOutputRoutine(self.currValue)", in that method's scope, the
local name "value" points to the same object as self.currValue does.
When you do "value = value + 1", the local name "value" now points to a
different object. That has no bearing on self.currValue.

Err, I can't find a guide here. Um, read the language spec? I dunno.

However:
>>my_list = [1]
def function(l):
.... l.append(2)
>>function(my_list)
my_list
[1, 2]

That's because function() is *mutating* the list; it's not changing what
the "l" name points to. It's calling the "append" method of the list
object, which changes said list object. If it were doing, say, "l = 42",
that would only rebind the function's local name "l":
>>my_list = [1]
def function(l):
.... l = 42
>>function(my_list)
my_list
[1]

Note that strings and integers are immutable, so whenever you think
you're mutating them (e.g. "s.replace('a', 'b')" or "i += 1"), you're
actually getting a whole new, different object, with all that that implies.
--
Jun 27 '08 #2
On Jun 19, 6:37 pm, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
monkeyboy wrote:
Hello,
I'm new to python, and PythonCard. In the code below, I'm trying to
create a member variable (self.currValue) of the class, then just pass
it to a simple function (MainOutputRoutine) to increment it. I thought
Python "passed by reference" all variables, but the member variable
doesn't seem to be incremented. If I make the function just increment
the member variable directly, without passing it in, it works fine?
In the code below, "self.currValue" stays at 5, and value evaluates to
1? Any insight would be appreciated...
class TestModel(model.Background):
def on_initialize(self,event):
self.currValue = 5
def on_startBtn_mouseClick(self, event):
self.MainOutputRoutine(self.currValue)
self.OutputRoutine(self.currValue)
def OutputRoutine(self,value):
self.components.txtBox1.text = str(value)
def MainOutputRoutine(self,value):
value = value + 1

That's not how Python works. When you call
"self.MainOutputRoutine(self.currValue)", in that method's scope, the
local name "value" points to the same object as self.currValue does.
When you do "value = value + 1", the local name "value" now points to a
different object. That has no bearing on self.currValue.

Err, I can't find a guide here. Um, read the language spec? I dunno.

However:
>my_list = [1]
def function(l):
... l.append(2)
>function(my_list)
my_list

[1, 2]

That's because function() is *mutating* the list; it's not changing what
the "l" name points to. It's calling the "append" method of the list
object, which changes said list object. If it were doing, say, "l = 42",
that would only rebind the function's local name "l":
>my_list = [1]
def function(l):
... l = 42
>function(my_list)
my_list

[1]

Note that strings and integers are immutable, so whenever you think
you're mutating them (e.g. "s.replace('a', 'b')" or "i += 1"), you're
actually getting a whole new, different object, with all that that implies.
--
Thank you, I haven't used python for a couple of years, and I didn't
recall that aspect of the language. I'll have to dig out my O'Reilly
book,
Jun 27 '08 #3

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
5
by: Tim::.. | last post by:
How do I transfer the value of one variable in one sub to another variable in another sub??? Thanks
3
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
7
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable...
12
by: ed | last post by:
Hi there, I just remembered this from a few days ago, and was looking for a reason for it. I was trying to call a function as so: addEvent( whatever, whatever, functionName() ); and as I...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.