Guide to the python interp. source?

Terry Reedy tjreedy at udel.edu
Sat Jul 27 13:39:06 EDT 2002


"Tim Gahnström /Bladerman" <tim at bladerman.com> wrote in message
news:R5x09.17790$p56.5752496 at newsb.telia.net...
> I want to make all variables mutable.

In a real sense, Python does not have variables.  It has objects, each
of which has 1 or more named or unnamed references thereunto.  Which
do *you* think of as variables:  objects, references, or names?  If
objects, you should look to another language since immutability of
certain types of objects is an integral part of Python (the operation
of dicts, for instance.)  I believe most people think of variables as
names, which works best (fewest surprises) within the Python data
model when bound to immutable data types.  For example:

x = 2
...
y = x
... (many lines)
x = x*x
# what should now be the value of y?  2 or 4?
# immutability of ints guarantees 2, which is what most people expect

> When you want to make functions like inc(x) where inc is supposed to
> increase the value of x by one. How is that done in Python?

You 'mutate' names most easily by rebinding them to a new value with
the binding (assignment) operator '=' as in 'x = x+1', 'x+=1', or even
x=inc(x)'.  With work, you could make inc('x') work for global var x
by fiddling with the globals dict (but this cannot be done for local
function vars).  Note that in C, you would also have to 'quote' x,
though with the '&' operator, as in inc(&x).

> That is my view of CBR, maybe that is not the correct definition .

To me, call by reference means that the function gets a reference to
the actual argument instead of an anonymous copy.  However, for Python
immutables, the effect is the same, except for avoiding the creation
and usual destruction of the copy.

> and I don't need CBR

If you work with instead of fighting the Python data model, you can
probably do what you want without completely reworking the
interpreter.

Terry J. Reedy






More information about the Python-list mailing list