[Python-3000] Sky pie: a "var" keyword

Neil Toronto ntoronto at cs.byu.edu
Mon Oct 9 20:15:30 CEST 2006


Neil Toronto wrote:
> Marcin 'Qrczak' Kowalczyk wrote:
>   
>>   It should be:
>>
>> var x = 0
>> def f():
>>    print x
>>    x = 3
>>
>> for the global variable, and:
>>
>> var x = 0
>> def f():
>>    var x
>>    print x
>>    x = 3
>>
>> for the local variable.
>>
>> There are a few sane choices for the semantics of 'var':
>>
>> 1. The variable is visible from 'var' to the end of the scope.
>>    If 'var' doesn't specify an initial value, accessing the variable
>>    before it gets assigned to is an error.
>>
>> 2. The variable is visible from 'var' to the end of the scope.
>>    If 'var' doesn't specify an initial value, 'None' is used.
>>
>> 3. The variable is visible from the beginning to the end of the scope.
>>    Accessing the variable before the 'var' is executed is an error.
>>    If 'var' doesn't specify an initial value, 'None' is used.
>>   
>>     
>
> For the record, though I didn't present it this way in my initial 
> proposal, I like #1 and #2 better than #3. Otherwise, you'd get this 
> silliness:
>
> def f():
>    x = 3   # fine, because the magic 'var' down below creates it
>    var x = 0
>
>   

On the other hand...

var x = 0
def f():
    if sometest:
        var x = 3

    x = 2

which is seriously evil, because it's not clear which 'x' the last 
assignment refers to. Maybe merely seeing "var" in a scope makes it a 
local, but using it before the "var" is an error.

The other option is making nested blocks have their own scope, but I 
don't think Python wants to go there.

Neil



More information about the Python-3000 mailing list