Returning a variable reference

Tom Culliton culliton at
Wed Nov 24 07:18:39 CET 1999

In article <slrn83mcos.j3.hutton at>,
Ross Hutton <hutton at> wrote:
>Ahh, I think I've just seen the light. Rather than trying to pass an
>lvalue up the chain I should be passing the rrr code down to lower
>access functions (get(rrr) and set(rrr,value)) and have them do the
>dirty work.

Or, since you can pass a tuple anywhere you can pass an index, just
compose and pass (mem, idx) or (reg, idx) around as your destination.
It does take a while to get over the C mentality and realize that
multi-element return values or parameters are both easy and natural in

>Many thanks again to Tom and Ben for their suggestions. You got me talking
>to myself and that's probably not such a bad thing. 
>It's quite interesting learning Python which is in many ways a lovely
>language (problem is that I keep thinking in C). I was amazed when I
>did some function pointer work without even looking in a manual. I
>just assigned a function to a variable and called the variable as a
>function. I just wrote code the way I thought it should work (and was
>quite surprised when it did).

More C mindset to unlearn, try to stop thinking of them as pointers.
EVERYTHING in python is passed (and returned) uniformly by reference,
some are immutable like numbers, strings, tuples, functions, others
are mutable like lists, dictionaries, class objects, most extension
types, ...

Maybe you've heard Lisp people talk about functions being "first class
objects"?  This is also true in Python, any "basic" operation you can
do with other types you can do with a function, "assign" it to a
variable, pass it as a parameter, return it, ...

Each type also has their own special operations like indexing sequence
and mapping objects, dumping the keys of a mapping, bitwise operations
on integers, calling functions, ...

It's easy to get fooled by how familiar "assignment" looks, when it's
really a very different mechanism.  Python doesn't have variables as
you know them in C.  A C variable name refers directly to a piece of
storage of a particular size and shape, but in Python it's an indirect
binding to an object of any type.  In C when you say:

x = x + 1

It fetches data from the location refered to by "x" adds 1 to that
value, and stores it back in the same location.  Where in Python, it
looks up "x" in a dictionary, eventually calls the add function with
references to that object and another object containing the integer
value 1, creates a new object containing the result, modifies the
dictionary to point to it, and if the reference count on the object
that x used to refer to has gone to zero garbage collects it.

In C the things you need to internalize to really get it are pointer
math and the central position of expressions.  When you've done this
things like:

char *p, *q; int i;

for (i = 0; i[p] = *(q + i); ++i)

are not suprising, just in bad taste.  To really get Python you need
to internalize the mutable/immutable distinction, and the central
place of dictionaries and references.

Oh well, thats probably way more preaching than you wanted. ;-)


More information about the Python-list mailing list