"pass by reference?"

Erik Max Francis max at alcyone.com
Sat Feb 23 04:18:31 EST 2002


Tripp Scott 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])

I would make a class to do the job (note, code untested):

	class Wrapper:
	    def __init__(self, x):
	        self.x = x

	    def get(self): return self.x
	    def set(self, x): self.x = x

	def make_abs(wrapper):
	    wrapper.set(abs(wrapper.get()))

	w = Wrapper(-1)
	make_abs(w)
	print w.get()

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list