Emulating C++ namespaces with ChainMap and metaclass trickery
Mark Adam
dreamingforward at gmail.com
Wed Oct 3 16:50:08 EDT 2012
On Wed, Oct 3, 2012 at 1:26 PM, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:
> C++ namespaces are useful for encapsulating related objects within a
> single file, subdividing the global namespace without using classes.
> Python has modules, but they come in separate files.
>
> Using Python 3.3's ChainMap type, and some metaclass trickery, I abuse
> the class keyword to (almost) emulate C++ namespaces:
>
Very interesting. I like the idea of continuing the namespace meme.
My idea of using the builtins (in the prior list thread of "namespaces and
modules"), is that if we overhaul the builtins, a unified data model could
emerge to incorporate whatever ideas one may have for namespaces (i.e.
"enclosures with a name").
My idea was to introduce the compound data type (using a ":" colon to
separate two sides), whereby one associates a (*hashable*) "name" with an
object ("meals":{"breakfast","lunch","dinner"}) . This has the extra
advantage of killing two warts in Python with one stone: {} now is the
empty set literal like people are taught, and a set of compounds makes a
dictionary (dict now has set operations available), something which, in
theory, should simply CPython implementation AND the python environment/API.
"expose name" put the dictionary (or whatever type is decided for the rhs)
into the builtin/global namespace.
I have further thoughts, but that's all I have at the moment....
markj
gothenburg, nebraska
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121003/9a33fb8e/attachment.html>
More information about the Python-list
mailing list