"no variable or argument declarations are necessary."

Duncan Booth duncan.booth at invalid.invalid
Wed Oct 5 07:16:47 EDT 2005


Antoon Pardon wrote:
> As far as I understand people don't like global very much so I don't
> expect that a second keyword with the same kind of behaviour has
> any chance.

That's why the behaviour I suggest is different than the current behaviour 
of global. Arguments against global (it is the only non-executable 
statement in Python & it is confusing because people don't understand the 
declaration goes inside the function instead of at global scope) don't 
apply.

> 
>> The 
>> 'global' keyword itself would be much improved if it appeared on the
>> same line as the assignment rather than as a separate declaration.
>>
>> e.g. something like:
>>
>> var1 = 0
>>
>> def f():
>>   var2 = 0
>>
>>   def g():
>>      outer var2 = 1 # Assign to outer variable
>>      global var1 = 1 # Assign to global
> 
> And what would the following do:
> 
> def f():
> 
>   var = 0
> 
>   def g():
> 
>     var = 1
> 
>     def h():
> 
>       outer var = 2 * var + 1
> 
>     h()
>     print var
> 
>   g()
>   print var
> 
> f()
> 
It would follow the principle of least surprise and set the value of var in 
g() of course. The variable in f is hidden, and if you didn't mean to hide 
it you didn't need to give the two variables the same name.

So the output would be:
3
0

(output verified by using my hack for setting scoped variables:)
-------------------------------
from hack import *
def f():
  var = 0

  def g():
    var = 1

    def h():
      assign(lambda: var, 2 * var + 1)

    h()
    print var

  g()
  print var

f()
-------------------------------



More information about the Python-list mailing list