On Sat, Aug 1, 2009 at 6:06 PM, Steven D'Aprano<st...@remove-this-cybersource.com.au> wrote: > I was playing around with a custom mapping type, and I wanted to use it > as a namespace, so I tried to use it as my module __dict__: > >>>> import __main__ >>>> __main__.__dict__ = MyNamespace() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: readonly attribute > > Why is __dict__ made read-only? > > I next thought I could change the type of the namespace to my class: > >>>> __main__.__dict__.__class__ = MyNamespace > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: __class__ assignment: only for heap types > > Drat, foiled again!!! > > Okay, if I can't do this at the module level, can I at least install a > custom namespace at the class level? <snip> > Apparently not. It looks like the namespace provided to the class > constructor gets copied when the class is made. <snip> > Is there any way to install a custom type as a namespace?
For classes/objects, yes, using metaclasses. See the __prepare__() method in PEP 3115: http://www.python.org/dev/peps/pep-3115/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list