[Python-ideas] Give nonlocal the same creating power as global

Jason H jhihn at gmx.com
Mon Sep 11 10:26:10 EDT 2017



> Sent: Monday, September 11, 2017 at 10:03 AM
> From: "João Matos" <jcrmatos at gmail.com>
> To: python-ideas at python.org
> Subject: [Python-ideas] Give nonlocal the same creating power as global
>
> Hello,
> 
> I would like to suggest that nonlocal should be given the same creating 
> power as global.
> If I do
> global a_var
> it creates the global a_var if it doesn't exist.
> 
...

I think this is a bad idea overall. It breaks encapsulation. 

I would suggest you use create_vars() to return a context:

context = create_vars()
create_layout(context)
create_bindings(context)

Or use a class:
class TkView:
def __init__(self):
   self.tk = Tk()
   self.create_layout()
   self.create_bindings()
def create_layout(self):
   # use self.tk
def create_bindings(self):
   # use self.tk


More information about the Python-ideas mailing list