"pass by reference?"

Tripp Scott tripps81 at yahoo.com
Tue Feb 26 14:43:23 EST 2002


At 24/02/2002 06:16, Magnus Lyckå wrote:
>>Suppose I want to make a function that modifies its argument:
>>  a = -1
>>  make_abs(a)
>>  print a # = 1
>>What is the Pythonic way of doing this? Wrap it in a list?
>>  make_abs([a])
>
>You could make 'a' point to a mutable variable, such as
>a list.
>
>def make_abs(l):
>     return map(abs,l)
>
>a = [-1]
>make_abs(a)
>print a[0] # 1

Ah, yes. Thanks for the correction.

>a = abs(a) is the Pythonic way to do it as others have said.

Got it. Guess I was still thinking in terms of PHP's 
func(&$foo). Also, thanks for the juicy and lengthy explanation!

t





More information about the Python-list mailing list