Emulating C++ namespaces with ChainMap and metaclass trickery

Discussion in 'Python' started by Steven D'Aprano, Oct 3, 2012.

  1. 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
    Steven D'Aprano, Oct 3, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G*rd*n

    Registry trickery?

    G*rd*n, Nov 6, 2004, in forum: HTML
    Replies:
    13
    Views:
    875
  2. ironfroggy
    Replies:
    16
    Views:
    428
    Michele Simionato
    Jun 3, 2005
  3. array copy via "struct-trickery"

    , Aug 28, 2007, in forum: C Programming
    Replies:
    8
    Views:
    322
    Keith Thompson
    Aug 31, 2007
  4. Paolo Nusco Perrotta

    Trickery in the ancestors chain

    Paolo Nusco Perrotta, Jan 24, 2007, in forum: Ruby
    Replies:
    9
    Views:
    136
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    298
Loading...

Share This Page