Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110907

Re: Namespaces are one honking great idea

From Ethan Furman <ethan@stoneleaf.us>
Newsgroups comp.lang.python
Subject Re: Namespaces are one honking great idea
Date 2016-07-01 09:00 -0700
Message-ID <mailman.2.1467388849.2295.python-list@python.org> (permalink)
References <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577693AC.6090807@stoneleaf.us>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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