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. Advertisements

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:
    1,000
  2. Craig Ringer
    Replies:
    1
    Views:
    3,774
    Andrew Dalke
    Jan 3, 2005
  3. ironfroggy
    Replies:
    16
    Views:
    621
    Michele Simionato
    Jun 3, 2005
  4. Jason
    Replies:
    4
    Views:
    1,976
    Jason
    May 7, 2007
  5. array copy via "struct-trickery"

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

    Trickery in the ancestors chain

    Paolo Nusco Perrotta, Jan 24, 2007, in forum: Ruby
    Replies:
    9
    Views:
    338
  7. J. Baute

    event trickery

    J. Baute, May 17, 2005, in forum: Javascript
    Replies:
    1
    Views:
    175
    Thomas 'PointedEars' Lahn
    May 24, 2005
  8. Steven D'Aprano

    Metaclass of a metaclass

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