[Python-ideas] Give nonlocal the same creating power as global
Chris Angelico
rosuav at gmail.com
Tue Sep 12 07:15:24 EDT 2017
More information about the Python-ideas mailing list
Tue Sep 12 07:15:24 EDT 2017
- Previous message (by thread): [Python-ideas] Give nonlocal the same creating power as global
- Next message (by thread): [Python-ideas] PEP 563: Postponed Evaluation of Annotations, first draft
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Sep 12, 2017 at 8:54 PM, Nick Coghlan <ncoghlan at gmail.com> wrote: > On 12 September 2017 at 03:32, João Matos <jcrmatos at gmail.com> wrote: >> Hello, >> >> You're correct. The idea is to give nonlocal the same ability, redirect >> subsequent bindings if the variable doesn't exist. > > The issue you're facing is that optimised local variables still need > to be defined in the compilation unit where they're locals - we're not > going to make the compiler keep track of all the nonlocal declarations > in nested functions and infer additional local variables from those. > (It's not technically impossible to do that, it just takes our already > complex name scoping rules, and makes them even more complex and hard > to understand). > > So in order to do what you want, you're going to need to explicitly > declare a local variable in the scope you want to write to, either by > assigning None to it (in any version), or by using a variable > annotation (in 3.6+). Future readers of your code will thank you for > making the widget definitions easier to find, rather than having them > scattered through an arbitrarily large number of nested functions :) Or, yaknow, the OP could actually use a class, instead of treating a closure as a poor-man's class... Honestly, I don't see much advantage to the closure here. A class is a far better tool for this job IMO. ChrisA
- Previous message (by thread): [Python-ideas] Give nonlocal the same creating power as global
- Next message (by thread): [Python-ideas] PEP 563: Postponed Evaluation of Annotations, first draft
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list