How to Teach Python "Variables"

Andrew Koenig ark at acm.org
Sun Nov 25 14:51:08 EST 2007


"Aurélien Campéas" <spamless.aurelien.campeas at free.fr> wrote in message 
news:4749c888$0$14869$426a74cc at news.free.fr...

> I mean : aren't C variables also bindings from names to objects ? Or what 
> ?

No, they're not.

In C, when you execute

    x = y;

you cause x to become a copy of y.  In Python, when you execute

    x = y

you cause x and y to be two different names for the same object.

It is difficult to distinguish these cases unless the objects in question 
are mutable, so consider this C example:

    struct {int a, b;} x = {3, 4}, y = x;
    x.a = 42
    printf ("%d, %d\n", y.a, y.b);

and the following apparently analogous Python example:

    x = [3, 4]
    y = x
    x[0] = 42
    print y

Try them both and see how they behave.






More information about the Python-list mailing list