[Python-ideas] global and nonlocal with atributes

Terry Jan Reedy tjreedy at udel.edu
Sun May 5 07:36:45 CEST 2013


On 5/4/2013 1:47 PM, João Bernardo wrote:
> Hi,
> I couldn't find whether this was proposed or not, but seems interesting
> to me:
>
> Whenever there's an assignment inside a big function,

If a function is 'too big', it can be split. Nexted functions that 
rebind a nonlocal name (closures), which is more often sensible than 
rebinding global names, are and should usually be short enough already.

> we think it is a
> local variable, but it could have been defined as global on top and that
> can mess things up if not checked.
>
> So, I why not have attribute syntax on "global" and "nonlocal" keywords?

Because attributes are attributes of objects and keywords are not objects

> Something that is written like:
>
> x = 10
> def increment():
>      global x
>      x += 1
>
> could be replaced by
>
> x = 10
> def increment():
>      global.x += 1

One can already do something similar
g = globals()
...
g['x'] += 1

--
Terry Jan Reedy





More information about the Python-ideas mailing list