Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > var or inout parm?

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
>

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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?

Your function returns None.

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

Colin W.
 
Reply With Quote
 
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?
>
> Your function returns None.
>
> The function in effect takes a copy of n.


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

 
Reply With Quote
 
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?
>>
>> Your function returns None.
>>
>> The function in effect takes a copy of n.

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


See Also: the earlier heated debate thread over what evaluation
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
>

 
Reply With Quote
 
Aaron Brady
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
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'.

>
> See Also: the earlier heated debate thread over what evaluation
> strategy Python uses (Survey says!: call-by-object).
>


Oh dear God. PLEASE don't see also that thread. That way lies madness.
Just google for call-by-object, and ignore the hell out of that thread.



> Cheers,
> Chris
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting an inout port to another inout port THurkmans VHDL 14 08-11-2009 08:32 PM
inout to inout Ken VHDL 2 05-09-2008 01:56 PM
connecting std_logic inout ports and std_logic_vector inout port =?ISO-8859-15?Q?Fr=E9d=E9ric_Lochon?= VHDL 3 11-08-2007 03:55 AM
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM



Advertisments