How to Teach Python "Variables"

none "atavory\" at (none)
Sun Nov 25 14:31:56 EST 2007


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.



More information about the Python-list mailing list