Pass-by-reference : Could a C#-like approach work in Python?

Erik Max Francis max at alcyone.com
Wed Sep 10 16:04:01 EDT 2003


Stephen Horne wrote:

> Its not an issue of whether the same effect can be achieved in Python.
> It's more an issue of whether the method used expresses the
> programmers intentions properly.

I agree.  And with your suggestion, now every single function call can
potentially change meaning.  Given Python's dynamicism, you may not even
know if this particular function call exhibits the behavior.  Now
Python's nice, predictable behavior is up for grabs.

If you want to rebinding outside the function, a far better way of
"expressing the programmer's intentions properly," in my opinion, is to
make the behavior as explicit as possible:

	class Container:
	    def __init__(self, x=None): self.x = x
	    def get(self): return self.x
	    def set(self, x): self.x = x
	    def increment(self): self.x += 1 # for example

	def Inc(c):
	    container.increment()

	container = Container(1)
	Inc(container)
	print container.get()

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Dead men have no victory.
\__/  Euripides




More information about the Python-list mailing list