pythong referencing/aliasing

Jeff Epler jepler at unpythonic.net
Fri Apr 25 17:32:21 CEST 2003


A simple assignment such as
    v = expr
only re-binds v to the result of expr.  There is thus no way to directly
translate C code such as
    void set_to_one(int &x) { 
        x = 1;
    }
into Python.

A complex assignment, such as
    x.y = expr
    x[y] = expr
does not re-bind the name "x".  Instead, it mutates (changes) the thing
named by x.  If something else names the same object, it will continue
to refer to the same object (with a new value).

Of course, only some objects are mutable in Python.  Others (strings,
tuples, and numbers) are immutable.

This is a fairly common FAQ.  You may find that reading the FAQ will
give you a clearer explanation.  I can only add one other piece of
information: I rarely am hurt by the fact that Python doesn't have
references of the type you're talking about.

Jeff





More information about the Python-list mailing list