Is it possible to pass a parameter by reference?

A

Anthony Liu

I defined two functions, f1 and f2.

f1 modifies the value of a variable called apple.

I want to pass the modified value of apple to f2.

How can I do this? I got stuck.




__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
 
M

M.E.Farmer

Anthony said:
I defined two functions, f1 and f2.

f1 modifies the value of a variable called apple.

I want to pass the modified value of apple to f2.

How can I do this? I got stuck.


Py>def f1(apple):
.... apple += 1
.... f2(apple)

py>def f2(apple):
.... print 'you have %s apples' % apple

py>f1(4)
'you have 5 apples'
Read the docs this is pretty basic, it will save you time.
If in doubt try it out.
Use the interpreter, it is your friend.
hth,
M.E.Farmer
 
T

Tim Roberts

Anthony Liu said:
I defined two functions, f1 and f2.

f1 modifies the value of a variable called apple.

I want to pass the modified value of apple to f2.

How can I do this? I got stuck.

It depends on the data type. Essentially, all objects are passed by
reference. However, some objects can be modified, and some cannot. Lists
can, strings and tuples cannot.

This is one of the most important thing to understand about Python, in my
opinion. There is a fundamental separation between an object and whatever
names it is known by.

Here's an example:

def xxx(yyy):
yyy[1] = 3

zzz = [ 0, 1, 2 ]
xxx(zzz)

After this code runs, zzz will be [0,3,2]. Note that, while xxx is
running, the list that was created as [0,1,2] has two names.

However, this code will fail within xxx, because the string cannot be
modified:
aaa = "abcde"
xxx(aaa)

However, watch this:

def xxx(yyy): # 0
yyy = [ 3, 4, 5] # 1

zzz = [ 0, 1, 2 ] # 2
xxx(zzz) # 3

After this runs, zzz will still be [0,1,2]. At line 2, a list object is
created containing [0,1,2]. The variable name zzz is bound to that list.
When we get to lines 3 and 0, the variable name yyy within function xxx is
bound to that exact same object.

However, in line 1, a NEW list object is created, containing [3,4,5]. The
variable yyy is then bound to this NEW object, and its binding to the other
list is lost. However, the variable zzz is still bound to [0,1,2].

If you need to create a new object and return it to the mainline, do that:

def xxx(yyy):
yyy = [ 3, 4, 5 ]
return yyy

zzz = [ 0, 1, 2 ]
zzz = xxx(zzz)
 
P

Paddy

It is usually clearer to explicitely return values that are changed by
a function and re-assign it to the same variable,

x=something1
a = something2
def f1(s,t):
# do something with t,
# do something to s
return s
a = f1(a,x)

Be aware however that you can wrap 'a' in a list for the same effect,
(but it is not as easy to read).

x=something1
aa = [something2]
def f2(ss,t):
s= ss[0]
# do something with t,
# do something to s
ss[0]=s
f2(aa,x)
a=aa[0]

-- Pad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top