Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110907
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| 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 | <mailman.2.1467388849.2295.python-list@python.org> (permalink) |
| 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 | <ethan@stoneleaf.us> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=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 |
Show key headers only | View raw
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~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 00:13 +1000
Re: Namespaces are one honking great idea Random832 <random832@fastmail.com> - 2016-07-01 10:40 -0400
Re: Namespaces are one honking great idea BartC <bc@freeuk.com> - 2016-07-01 15:49 +0100
Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-02 03:13 +1000
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 03:46 +1000
Re: Namespaces are one honking great idea Rustom Mody <rustompmody@gmail.com> - 2016-07-01 19:26 -0700
Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-01 09:00 -0700
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 03:10 +1000
Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-01 12:29 -0700
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 11:07 +1000
Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-02 01:50 +0000
Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-01 19:55 -0700
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-03 13:22 +1000
Re: Namespaces are one honking great idea Random832 <random832@fastmail.com> - 2016-07-02 00:59 -0400
Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-02 15:34 +0000
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-03 13:44 +1000
Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-02 22:14 -0700
Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-03 22:02 +0000
Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 16:21 -0700
Re: Namespaces are one honking great idea BartC <bc@freeuk.com> - 2016-07-04 01:19 +0100
Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-03 21:01 -0700
Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-02 16:28 -0700
Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-04 21:37 +1000
Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-04 15:09 -0700
Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-04 15:11 -0700
Re: Namespaces are one honking great idea jmp <jeanmichel@sequans.com> - 2016-07-04 17:58 +0200
Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-05 12:58 +1000
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-05 13:35 +1000
Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-05 13:47 +1000
Re: Namespaces are one honking great idea Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-07-05 16:09 +1000
A nestedmodule decorator (Re: Namespaces are one honking great idea) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-07-05 21:10 +1200
Improved nestedmodule decorator implementation Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-07-05 21:31 +1200
Re: Improved nestedmodule decorator implementation Steven D'Aprano <steve@pearwood.info> - 2016-07-05 20:54 +1000
Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-05 12:34 +1000
Re: Namespaces are one honking great idea jmp <jeanmichel@sequans.com> - 2016-07-04 13:23 +0200
Re: Namespaces are one honking great idea carlosjosepita@gmail.com - 2016-07-09 07:05 -0700
csiph-web