Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: Namespaces are one honking great idea Date: Fri, 01 Jul 2016 09:00:44 -0700 Lines: 120 Message-ID: References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577693AC.6090807@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de eqbp+sccKL2u+bYiaoWtfAK8JKbq54xvshxlrbhzDi7g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; '*not*': 0.07; 'practice,': 0.07; 'type,': 0.07; '"class"': 0.09; 'builtins': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'globals': 0.09; 'message-id:@stoneleaf.us': 0.09; 'namespace': 0.09; 'simplified': 0.09; 'example:': 0.10; 'python': 0.10; 'python.': 0.11; 'syntax': 0.13; 'def': 0.13; 'variables': 0.15; 'feasible': 0.16; 'file?': 0.16; 'function?': 0.16; 'magic': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'metaclasses.': 0.16; "module's": 0.16; 'namespace,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'subject:Namespaces': 0.16; 'syntactic': 0.16; 'test():': 0.16; 'unresolved': 0.16; 'wrote:': 0.16; 'abuse': 0.18; '(in': 0.18; 'skip:= 10': 0.18; 'decorator': 0.22; 'skip:= 20': 0.22; 'am,': 0.23; "python's": 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'module.': 0.27; 'skip:e 30': 0.27; 'function': 0.28; 'idea': 0.28; 'this.': 0.28; 'resolution': 0.28; 'argue': 0.29; 'be:': 0.29; 'idea,': 0.29; '~ethan~': 0.29; 'comments': 0.30; 'that.': 0.30; 'probably': 0.31; 'especially': 0.32; 'embedded': 0.32; 'ideal': 0.32; 'statement': 0.32; 'class': 0.33; "d'aprano": 0.33; 'limitations': 0.33; 'steven': 0.33; 'skip:d 20': 0.34; 'add': 0.34; 'world,': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'depends': 0.36; 'keyword': 0.36; 'limitation': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'version': 0.38; 'names': 0.38; 'several': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'questions': 0.40; 'close': 0.61; 'provide': 0.61; 'charset:windows-1252': 0.62; 'needing': 0.63; 'more': 0.63; 'different': 0.63; 'great': 0.63; 'believe': 0.66; 'better.': 0.66; 'below.': 0.66; 'fact,': 0.67; 'biggest': 0.67; 'lack': 0.76; 'concept.': 0.84; 'zen': 0.84; 'choices.': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <577693AC.6090807@stoneleaf.us> X-Mailman-Original-References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:110907 On 07/01/2016 07:13 AM, Steven D'Aprano wrote: I like the idea, but I have a couple questions about the design choices. Comments below. > The Zen of Python says: > > Namespaces are one honking great idea -- let's do more of those! > Proposal > ========= > > Add a new "namespace" object to Python. > Proof of concept > ================= > > Here's a proof of concept. I use a class with a custom metaclass like this: > > > # Python 3 version > class ExampleNS(metaclass=Namespace): > x = 1 > y = [] > > def spam(n): > return 'spam'*n > > z = spam(3).upper() > > def ham(n): > return spam(n).replace('sp', 'H') > > def test(): > global x > x += 1 > y.append(1) > return x, y > > Despite the "class" statement (a limitation of Python's lack of dedicated > syntax for namespaces), the Example namespace behaves like (in fact, *is*) > a module embedded inside a module. So the idea is to have several "mini-modules" inside a single file? Can a mini-module function access a different mini-module's function? Can a mini-module function access/interact with the module's global scope? > Especially note that inside the namespace, the global statement makes a name > refer to the namespace scope. Interesting. :) > Unresolved questions > ===================== > > Would a decorator be better than a metaclass? I think a decorator may provide more of a clue that something interesting is happening -- whether a decorator is feasible depends on whether you need the __prepare__ magic of metaclasses. > @namespace > class Example: > ... I believe Python already has a NameSpace type, so a different name is probably better. How about minimodule? ;) > Some limitations > ================= > > The simplified implementation shown doesn't allow functions inside the > namespace to access names outside of the namespace easily. How not-easy is it? > In practice, the > user might want the name resolution order inside the namespace to be: > > local variables > nonlocal variables > namespace globals > module globals > builtins That would be ideal, I think. How close can we get to that? > The biggest limitation is that I have to abuse the class statement to do > this. In an ideal world, there would be syntactic support and a keyword: > > namespace Example: > x = 0 > y = [] > def test(n): ... > > although others might argue that *not* needing a dedicated keyword is, in > fact, better. Metaclasses for the win! > Disadvantages > ============== > > Those unfamiliar with the namespace concept may be confused by a class that > doesn't get instantiated. A well-named decorator/base-class should help with that. Did you mean for this to go to -Ideas? -- ~Ethan~