[IronPython] Simulating namespaces

Michael Foord fuzzyman at voidspace.org.uk
Mon Sep 25 18:11:53 CEST 2006


David Anton wrote:
> Is this an acceptable way to simulate the type of namespace you'd define in
> C#, VB, etc?
>
> class SimulatedNamespace:
>     class SomeClass(object):
>         ....
>
> It seems to work ok in my tests, but is there some pitfall to doing this?
>   
For pure Python code I'd use a module instead of a namespace - but the 
syntax isn't as nice.

class SomeClass(object):
    pass

import imp
mod = imp.new_module("name")
mod.SomeClass = SomeClass

Your way is fine, if a slightly odd use of the class statement - but a 
module is a better match for the namespace concept in Python terms. The 
downside of a module is that it will also contain the builtins. The 
downside of a class is that it will have some default methods.

Michael Foord
http://www.voidspace.org.uk/python/index.shtml




More information about the Ironpython-users mailing list