anything like C++ references?

Roy Smith roy at panix.com
Sun Jul 13 10:30:05 EDT 2003


Stephen Horne <intentionally at blank.co.uk> wrote:
> One of the few things I hate about Python is that mutable objects are
> implicitly shared using a reference system whereas immutable objects
> are not. It is unnecessarily confusing and error prone.

There's a bit of confusion in the above regarding mutability vs. call by 
reference (they're orthogonal ideas), but I think I know what you were 
trying to say.

In ancient versions of Fortran, even constants (i.e. immutables) were 
pass by reference.  You could write something like this (forgive me if 
I've forgotten some of the syntax details):

      subroutine x (i)
      i = 42
      end

      subroutine main
      x (5)
      write (6, 100) 5
100   format (1i6)
      end

and it would print 42.  There was a single instance of the constant 5 
for main, and it was passed by reference to x(), which in turn changed 
its value to 42.  I don't remember if Fortan 66 did this, or if that 
only happened in even older versions.  Talk about confusing and error 
prone!

The Python version of that would be:

def mutateString (s):
    s = "some other string"

s ("foo")
print "foo"

If strings were pass by reference, the above would print "some other 
string".  Unless, of course, you got away from the idea that multiple 
instances of a string constant with the same value are really the same 
object, in which case:

x = "foo"
x is "foo"

would print 0 instead of 1 like it does now.




More information about the Python-list mailing list