How to Teach Python "Variables"

Hrvoje Niksic hniksic at xemacs.org
Mon Nov 26 04:49:46 EST 2007


greg <greg at cosc.canterbury.ac.nz> writes:

> none wrote:
>>     IIRC, I once saw an explanation how Python doesn't have
>> "variables" in the sense that, say, C does, and instead has bindings
>> from names to objects.
>
> If you're talking to C programmers, just tell them that Python
> variables always contain pointers. That should give them the right
> mental model to build on.

That is a convenient shortcut when it works, but in my experience it
tends to confuse the issue.  The reason is that one of the main uses
of pointers in C is implementing pass-by-reference.  A C programmer
told that Python variables internally hold pointers expects this code:

def func(a):
  a = 10
...
func(x)

to change the value of x.



More information about the Python-list mailing list