"no variable or argument declarations are necessary."

Antoon Pardon apardon at forel.vub.ac.be
Wed Oct 5 06:19:31 EDT 2005


Op 2005-10-05, Duncan Booth schreef <duncan.booth at invalid.invalid>:
> Antoon Pardon wrote:
>
>> It also is one possibility to implement writable closures.
>> 
>> One could for instace have a 'declare' have the effect that
>> if on a more inner scope such a declared variable is (re)bound it
>> will rebind the declared variable instead of binding a local name.
>
> That is one possibility, but I think that it would be better to use a 
> keyword at the point of the assigment to indicate assignment to an outer 
> scope. This fits with the way 'global' works: you declare at (or near) the 
> assignment that it is going to a global variable, not in some far away part 
> of the code, so the global nature of the assignment is clearly visible.

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.

> 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()



More information about the Python-list mailing list