How to Teach Python "Variables"

hdante hdante at gmail.com
Tue Nov 27 10:06:13 EST 2007


On Nov 25, 5:31 pm, none <""atavory\"@(none)"> wrote:
> Aurélien Campéas wrote:
> > none a écrit :
> >>     Hello,
>
> >>     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. Does anyone have a link?
>
> >>     Thanks,
>
> >>     Ami
>
> > That's something I've often heard and I don't get it. Somehow I don't
> > understand how C variables are not like python bindings (the differences
> > being that C variables are statically typed and completely disappear at
> > run-time; are these differences important enough to warrant such a shift
> > in terminology ? (yes there are some other differences, but then the
> > question is asked in a context of pedagogy, where the audience is
> > introduced to the basics))
>
> > I mean : aren't C variables also bindings from names to objects ? Or what ?
>
> Thanks.
>
> It's very possible you're right - I don't know. There seem to be some
> differences however. To name a few:
> 1. A C variable exists regardless of whether you're storing something in
> it. Not so for a python "variable" - so it's a bit really more like a
> name for something that exists independently.
> 2. C variables (or C++ objects) completely disappear when out of scope,
> but that's not necessarily true of Python objects.
> 3. C++ references have the semantics that if a = b, and you write a.c =
> 3, then b.c == 3. This is also true in Python. But then if a = b, and
> then you write b = 5, then a is still bound to the original value of b,
> so it's not exactly like a reference.
> 4. Etc.
>
> So on the one hand, you're obviously right, and maybe there's no room
> for a paradigm shift. OTOH, if the simplest explanation is "it's like a
> C/C++ variable/reference/pointer except for 1, 2, 3, 4,...", then maybe
> it is different. I just don't know, hence my question.

 (If I say something wrong, please raise your hand)
 For me, python variables are _exactly_ the same as reference counted
(or garbage collected) C++ pointers. It's just that the assignment
operator in python has distinctive semantics.

 for example: the following code in python

 a = 3
 print a + 2
 print len(a)
 b = a
 print id(a), id(b)
 print a + b
 a = 5

 is similar to the following pseudo-code in C++

 object *a, *b;

 // a = 3
 a = refnew<Int>(3);

 // print a + 2
 print(a->add(*refnew<Int>(2)));

 // print len(a)
 cout << len(*a) << '\n';

 // b = a
 b = refnew<object>(a);

 // print id(a), id(b)
 print(*refnew<List>(a, b))

 // print a + b
 print(a->add(*b));

 // a = 5
 refdel(a);
 a = refnew<Int>(5);

 So, we can see that, in python:
 - attributions operate in the pointer
 - method calls (that is, everything else, except id()) operate in the
object

 And that's it. I think that there is confusion because everything we
do with python variables are pointer dereferences, except for the
attribution, that instead of dereferencing and operating the object,
just changes the pointer. The scope is just a matter of using
automatic memory management or not.



More information about the Python-list mailing list