Emulating C++ namespaces with ChainMap and metaclass trickery

2012-10-03 Thread Steven D'Aprano
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:

http://code.activestate.com/recipes/578279/




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Emulating C++ namespaces with ChainMap and metaclass trickery

2012-10-03 Thread Mark Adam
On Wed, Oct 3, 2012 at 1:26 PM, Steven D'Aprano 
steve+comp.lang.pyt...@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
-- 
http://mail.python.org/mailman/listinfo/python-list