I'm wrong or Will we fix the ducks limp?

Marko Rauhamaa marko at pacujo.net
Tue Jun 7 03:56:55 EDT 2016


Gregory Ewing <greg.ewing at canterbury.ac.nz>:

> Marko Rauhamaa wrote:
>> Seriously, though, it is notable that the high-level programming
>> languages pretty unanimously refuse to make variables first-class
>> objects. I wonder why.
>
> That's an interesting question. One reason might be
> that in the absence of static type analysis, assigning
> to a variable holding a reference to another variable
> would be ambiguous. For example, suppose Python had
> an & operator that gives you an object referring to
> a variable somehow. Then, after
>
> a = 42
> b = 17
> c = &a
> c = &b
>
> does 'c' now hold a reference to the variable 'b', or
> does it still hold a reference to 'a' and 'a' now
> holds a reference to 'b'?

If variables were ordinary mutable objects, you'd need a syntax of
dereferencing, just like in C. Variable objects would be highly
analogous to single-element arrays, little boxes if you will.

> Somehow these two operations would have to be spelled different ways,
> which means you would need to know whether you were dealing with a
> variable reference or not. So they wouldn't really be first-class, in
> the sense of being treated on an equal footing with ordinary
> variables.

It's not that ambiguous.

   >>> a = 3
   >>> c = &a
   >>> c
   <global variable a>
   >>> *c
   3
   >>> c is a
   False
   >>> *c is a
   True
   >>> c is &a
   True
   >>> a = 4
   >>> *c
   4
   >>> *c is a
   True
   >>> c = &c
   >>> c
   <global variable c>
   >>> *c
   <global variable c>
   >>> **c
   <global variable c>


Marko



More information about the Python-list mailing list