Velocity Reviews > var or inout parm?

# var or inout parm?

mh@pixar.com
Guest
Posts: n/a

 12-07-2008
How can I make a "var" parm, where the called function can modify
the value of the parameter in the caller?

def f(x):
x = x + 1

n = 1
f(n)
# n should now be 2

Many TIA!!
Mark

--
Mark Harrison
Pixar Animation Studios

Chris Rebert
Guest
Posts: n/a

 12-07-2008
On Sun, Dec 7, 2008 at 12:54 AM, <(E-Mail Removed)> wrote:
> How can I make a "var" parm, where the called function can modify
> the value of the parameter in the caller?

Not directly possible or encouraged. You can emulate it by sticking
the value in a container object (e.g. list) though:

def f(x):
x[0] += 1 #non-augmented assignment would work too

n = [1]
f(n)
print n[0] #==> 2

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

>
> def f(x):
> x = x + 1
>
> n = 1
> f(n)
> # n should now be 2
>
> Many TIA!!
> Mark
>
>
> --
> Mark Harrison
> Pixar Animation Studios
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Diez B. Roggisch
Guest
Posts: n/a

 12-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> How can I make a "var" parm, where the called function can modify
> the value of the parameter in the caller?
>
> def f(x):
> x = x + 1
>
> n = 1
> f(n)
> # n should now be 2

Chris showed one way, another is simply returning it. As python can
return ad-hoc created tuples & unpack them, some C++-wrappers for
example treat out/inout vars like this:

def foo(in, inout):
return inout, out1, out2

x, y, z = foo(a, x)

Obviously you are responsible for properly assigning the returned inout
to the right name again.

Diez

Steven D'Aprano
Guest
Posts: n/a

 12-07-2008
On Sun, 07 Dec 2008 08:54:46 +0000, mh wrote:

> How can I make a "var" parm, where the called function can modify the
> value of the parameter in the caller?

By using another language.

> def f(x):
> x = x + 1
>
> n = 1
> f(n)
> # n should now be 2

Python doesn't work like that. You should read this:

http://effbot.org/zone/python-objects.htm

Some work arounds, in order from worst-to-best:

(1) Use a hard-coded global name:

x = 1

def f():
global x
x = x + 1

f()
assert x == 2

(2) Wrap the value you want to change in a list, then modify the list in
place.

n = [1]

def f(alist):
alist[0] = alist[0] + 1

f(n)
assert n[0] == 2

(4) Just use an ordinary function. Functions can return multiple values.

n = 1

def f(x):
return (x+1, 99)

n, y = f(n)
assert y == 99
assert n == 2

(5) Find another way to solve your problem.

Why do you think you need var parameters? What problem are you hoping to
solve by using them? As a former Pascal programmer, I missed var
parameters at first, but now I don't.

--
Steven

Colin J. Williams
Guest
Posts: n/a

 12-07-2008
(E-Mail Removed) wrote:
> How can I make a "var" parm, where the called function can modify
> the value of the parameter in the caller?
>
> def f(x):
> x = x + 1
>
> n = 1
> f(n)
> # n should now be 2
>
> Many TIA!!
> Mark
>
>

Why not run it and see?

The function in effect takes a copy of
n. n is not changed.

Colin W.

Bruno Desthuilliers
Guest
Posts: n/a

 12-08-2008
Colin J. Williams a écrit :
> (E-Mail Removed) wrote:
>> How can I make a "var" parm, where the called function can modify
>> the value of the parameter in the caller?
>>
>> def f(x):
>> x = x + 1
>>
>> n = 1
>> f(n)
>> # n should now be 2
>>
>> Many TIA!!
>> Mark
>>
>>

>
> Why not run it and see?
>
>
> The function in effect takes a copy of n.

Nope, it takes the object bound to global name 'n'.

Chris Rebert
Guest
Posts: n/a

 12-08-2008
On Mon, Dec 8, 2008 at 12:26 AM, Bruno Desthuilliers
<(E-Mail Removed)> wrote:
> Colin J. Williams a écrit :
>>
>> (E-Mail Removed) wrote:
>>>
>>> How can I make a "var" parm, where the called function can modify
>>> the value of the parameter in the caller?
>>>
>>> def f(x):
>>> x = x + 1
>>>
>>> n = 1
>>> f(n)
>>> # n should now be 2
>>>
>>> Many TIA!!
>>> Mark
>>>
>>>

>>
>> Why not run it and see?
>>
>>
>> The function in effect takes a copy of n.

>
> Nope, it takes the object bound to global name 'n'.

strategy Python uses (Survey says!: call-by-object).

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Guest
Posts: n/a

 12-08-2008
On Dec 7, 2:54*am, (E-Mail Removed) wrote:
> How can I make a "var" parm, where the called function can modify
> the value of the parameter in the caller?
>
> def f(x):
> * * x = x + 1
>
> n = 1
> f(n)
> # n should now be 2

You can't. 'n' would need a 'set_to' method, which it doesn't have.
Otherwise, that's not what the equal sign does.

mh@pixar.com
Guest
Posts: n/a

 12-10-2008
Chris Rebert <(E-Mail Removed)> wrote:
> Not directly possible or encouraged. You can emulate it by sticking
> the value in a container object (e.g. list) though:

Thanks, that's just what I needed. I'm using this to monkeypatch
a test driver into some code that I can't touch, otherwise
I would just have the function return a tuple with the replacement
values.

Cheers!
Mark

--
Mark Harrison
Pixar Animation Studios

J. Clifford Dyer
Guest
Posts: n/a

 12-10-2008
On Mon, 2008-12-08 at 00:57 -0800, Chris Rebert wrote:
> On Mon, Dec 8, 2008 at 12:26 AM, Bruno Desthuilliers
> <(E-Mail Removed)> wrote:
> > Colin J. Williams a écrit :
> >>
> >> (E-Mail Removed) wrote:
> >>>
> >>> How can I make a "var" parm, where the called function can modify
> >>> the value of the parameter in the caller?
> >>>
> >>> def f(x):
> >>> x = x + 1
> >>>
> >>> n = 1
> >>> f(n)
> >>> # n should now be 2
> >>>
> >>> Many TIA!!
> >>> Mark
> >>>
> >>>
> >>
> >> Why not run it and see?
> >>
> >> Your function returns None.
> >>
> >> The function in effect takes a copy of n.

> >
> > Nope, it takes the object bound to global name 'n'.

>
> strategy Python uses (Survey says!: call-by-object).
>

Just google for call-by-object, and ignore the hell out of that thread.

> Cheers,
> Chris
>