I'm wrong or Will we fix the ducks limp?
BartC
bc at freeuk.com
Tue Jun 7 06:49:13 EDT 2016
On 07/06/2016 08:56, Marko Rauhamaa wrote:
> 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'?
c points to b. For the latter part of your statement to be true, the
last line might have to be something like:
*c = &b
>> 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>
Here are some results in another, non-Python language. While the object
reference module is similar to Python's, the language retains explicit
pointers which can be used for variable references.
In this code, ^x means pointer to x, x^ means dereference c, while := is
assignment and = means equality:
a := 3
c := ^a
println c # refvar: 02176228
println c = a # error (compare pointer to int)
println c = ^a # True
a := 4
println c^ # 4
c^ := 12
println a # 12
c := ^c
println c # refvar: 02176248
println c^ # refvar: 02176248
println c^^ # refvar: 02176248
Associating a pointer value to a variable symbol table entry is a
separate step. But a pointer needn't point at just a named variable:
a := (10,20,30,40,50)
c := ^a[3]
println c^ # 30 (was 1-based)
c^ := 77
println a # (10,20,77,40,50)
And here is the above example:
a := 42
b := 17
c := ^a
c^ := ^b
println a # refvar: 21A16F0
println a^ # 17
--
Bartc
More information about the Python-list
mailing list