The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by arnaud]

Hello,

This is probably a classic question but I could not find an answer in the
book, where the chapter on functions is not quite detailed on passing
arguments by variable to a function.
I have bought the book to switch from Tcl to Python which looks much better
for many things, but one thing that disappointed me is the fact that there
does not appear to be a possibility to pass arguments of a function as a
variable instead of as a value, except by declaring these variables globals,
like in Tcl.
I find this a bit of a downside. I sort of hoped it would simpler to do than
in C where it can be really confusing.

Am I telling lies here?

Arnaud.
import-bot (20211) [Avatar] Offline
#2
Re: Passing arguments by variable
[Originally posted by daryl harms]

> Hello,
>
> This is probably a classic question but I could not find an answer in the
> book, where the chapter on functions is not quite detailed on passing
> arguments by variable to a function.
> I have bought the book to switch from Tcl to Python which looks much better
> for many things, but one thing that disappointed me is the fact that there
> does not appear to be a possibility to pass arguments of a function as a
> variable instead of as a value, except by declaring these variables globals,
> like in Tcl.
> I find this a bit of a downside. I sort of hoped it would simpler to do than
> in C where it can be really confusing.
>
> Am I telling lies here?
>
> Arnaud.

Hi Arnaud,

I'm not sure I'm intepreting what you are referring to as pass by variable
correctly. But if what you want is to have your arguments set to new values
calculated in a function, you should be able to get this effect without having
to resort to global variables. You could have the new values returned in a
tuple, i.e.:

>>> def foo(p1,p2,p3):
p1 = p1 +1
p2 = p2 +2
p3 = p3 +3
return (p1,p2,p3)

>>> a1 =1
>>> a2 =2
>>> a3 =3
>>> a1,a2,a3 = foo(a1,a2,a3)
>>> a1
2
>>> a2
4
>>> a3
6
>>>

Note that you probably want to think of what Python is doing as pass by
reference rather than pass by value. That is the parameters (i.e. p1,p2, and
p3 above) are set to point (or refer) to the objects pointed to by the
arguments (i.e. a1,a2 and a3 above). Thus if an argument points to a mutable
object (such as a list, dictionary or class instance), then any changes made
to this object within the function through the parameter will be seen outside
the function when referring to this object through the argument:

>>> def foo2(list1):
list1.append(3)


>>> x = [1,2]
>>> foo2(x)
>>> x
[1, 2, 3]
>>>


Daryl