function arguments passed by reference

  • Thread starter Marcello Pietrobon
  • Start date
M

Marcello Pietrobon

Hello,

My background is C++

I would like to know for sure if it is possible to pass a value by
reference to a function AND having it changed by the function itself


def changeValue( text ):
text = 'c'
print text
return
'a'

Question 1)
I guess that in Python ( differently than in C++) somehow txt is passed
by reference even if its value is not changed.
Is this true ?

Question 2)
How can I define a function that changes the value of txt ( without
having to return the changed value ) ?

Question 3)
Is there some documentation talking extensively about this ?


Thank you,
Marcello
 
J

John Roth

Marcello Pietrobon said:
Hello,

My background is C++

I would like to know for sure if it is possible to pass a value by
reference to a function AND having it changed by the function itself


def changeValue( text ):
text = 'c'
print text
return

'a'

Question 1)
I guess that in Python ( differently than in C++) somehow txt is passed
by reference even if its value is not changed.
Is this true ?

Question 2)
How can I define a function that changes the value of txt ( without
having to return the changed value ) ?

Question 3)
Is there some documentation talking extensively about this ?

The key concept is that everything is an object, and all objects
are passed by reference in the sense that what's bound to the
funtion's parameters is the objects that were passed in.

However, you cannot affect the bindings in the calling environment.
Regardless of what you do (unless you do some deep magic with
the invocation stack) nothing is going to affect the bindings in the
caller.

So you can rebind anything you want to the parameters within
the function, and it will have no effect on the calling environment.

On the other hand, if you mutate a mutable object that was passed
as a parameter, that change will be visible after you return.

For example:

def foo(bar):
bar = "shazoom"

a = "shazam"
foo(a)

The result is that a is still "shazam."

However, if you do this:

def foo(bar):
bar.append("shazoom")

a = ["shazam"]
foo(a)

the result is:

["shazam", "shazoom"]

HTH
John Roth

"shazoom" ::= ["strength", "health", "aptitude", "zeal", "ox, power of",
"ox, power of another", "money"]

whatever the value you passed in remains.
 
M

Marcello Pietrobon

John said:
Hello,

My background is C++

I would like to know for sure if it is possible to pass a value by
reference to a function AND having it changed by the function itself


def changeValue( text ):
text = 'c'
print text
return

'a'

Question 1)
I guess that in Python ( differently than in C++) somehow txt is passed
by reference even if its value is not changed.
Is this true ?

Question 2)
How can I define a function that changes the value of txt ( without
having to return the changed value ) ?

Question 3)
Is there some documentation talking extensively about this ?

The key concept is that everything is an object, and all objects
are passed by reference in the sense that what's bound to the
funtion's parameters is the objects that were passed in.

However, you cannot affect the bindings in the calling environment.
Regardless of what you do (unless you do some deep magic with
the invocation stack) nothing is going to affect the bindings in the
caller.

So you can rebind anything you want to the parameters within
the function, and it will have no effect on the calling environment.

On the other hand, if you mutate a mutable object that was passed
as a parameter, that change will be visible after you return.

For example:

def foo(bar):
bar = "shazoom"

a = "shazam"
foo(a)

The result is that a is still "shazam."

However, if you do this:

def foo(bar):
bar.append("shazoom")

a = ["shazam"]
foo(a)

the result is:

["shazam", "shazoom"]

HTH
John Roth

"shazoom" ::= ["strength", "health", "aptitude", "zeal", "ox, power of",
"ox, power of another", "money"]

whatever the value you passed in remains.

Thank you for the answers,

difficult to get used to these concepts after coming from C++ !
But I am starting to understand:
the main difference between Python and C++ is that I need to keep in
mind what is mutable and what is not mutable in Python

Cheers,
Marcello
 
J

Josef Meile

Marcello said:
John said:
Hello,

My background is C++

I would like to know for sure if it is possible to pass a value by
reference to a function AND having it changed by the function itself


def changeValue( text ):
text = 'c'
print text
return

txt = 'a'
changeValue( txt )
'c'
print txt
'a'

Question 1)
I guess that in Python ( differently than in C++) somehow txt is passed
by reference even if its value is not changed.
Is this true ?

Question 2)
How can I define a function that changes the value of txt ( without
having to return the changed value ) ?

Question 3)
Is there some documentation talking extensively about this ?


The key concept is that everything is an object, and all objects
are passed by reference in the sense that what's bound to the
funtion's parameters is the objects that were passed in.

However, you cannot affect the bindings in the calling environment.
Regardless of what you do (unless you do some deep magic with
the invocation stack) nothing is going to affect the bindings in the
caller.

So you can rebind anything you want to the parameters within
the function, and it will have no effect on the calling environment.

On the other hand, if you mutate a mutable object that was passed
as a parameter, that change will be visible after you return.

For example:

def foo(bar):
bar = "shazoom"

a = "shazam"
foo(a)

The result is that a is still "shazam."

However, if you do this:

def foo(bar):
bar.append("shazoom")

a = ["shazam"]
foo(a)

the result is:

["shazam", "shazoom"]

HTH
John Roth

"shazoom" ::= ["strength", "health", "aptitude", "zeal", "ox, power of",
"ox, power of another", "money"]

whatever the value you passed in remains.

Thank you for the answers,

difficult to get used to these concepts after coming from C++ !
But I am starting to understand:
the main difference between Python and C++ is that I need to keep in
mind what is mutable and what is not mutable in Python
You could also return tupples like
.... a+=2
.... b+=3
.... return a,b
....5

Regards,
Josef
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top