[Python-ideas] global and nonlocal with atributes

João Bernardo jbvsmo at gmail.com
Sat May 4 19:47:22 CEST 2013


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, 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?
Something that is written like:

x = 10
def increment():
    global x
    x += 1

could be replaced by

x = 10
def increment():
    global.x += 1

another example:

def foo(x):
    def bar():
        return nonlocal.x + 1
    return bar

These should generate the same "LOAD_GLOBAL" and "LOAD_DEREF" bytecodes, so
"global" and "nonlocal" will not be objects nor passed as arguments.

That way you can have both "global.x", "nonlocal.x" and "x" variables
without conflict.

x = 10
def f(x):
   def g():
       # how can I set both "x" variables inside here?

"Namespaces are one honking great idea -- let's do more of those!"

--
João Bernardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130504/6d2f8563/attachment.html>


More information about the Python-ideas mailing list