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

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Oct 9 16:27:45 CEST 2006


"Jim Jewett" <jimjjewett at gmail.com> writes:

>> A "var" keyword fixes them all. The "global" gotcha:
>
>> x = 0
>> def f():
>>    print x   # no exception - prints "0"
>>    x = 3
>
> But which of the above would this mean?

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 people coming from another language, "var" isn't likely to mean
> "by the way, this gets modified elsewhere".

This aspect is perfectly clear for me. Why would it be disallowed
to be modified elsewhere?

> The existing idiom of
>
>     def f():
>         x = [0]
>         def g():
>             x[0] = 3
>
> is at least strange enough to warn people that they need to read carefully.

This idiom is a silly workaround for the lack of explicit variable
definitions, and thus for the inability of modifying variables defined
in outer scopes.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


More information about the Python-3000 mailing list