[Python-Dev] Assignment to __class__ of module? (Autoloading? (Making Queue.Queue easier to use))

Phillip J. Eby pje at telecommunity.com
Thu Oct 13 04:16:57 CEST 2005


At 01:47 PM 10/13/2005 +1300, Greg Ewing wrote:
>I just tried to implement an autoloader using a technique
>I'm sure I used in an earlier Python version, but it no
>longer seems to be allowed.
>
>I'm trying to change the __class__ of a newly-imported
>module to a subclass of types.ModuleType, but I'm getting
>
>    TypeError: __class__ assignment: only for heap types
>
>Have the rules concerning assignent to __class__ been
>made more restrictive recently?

It happened in Python 2.3, actually.  The best way to work around this is 
to add an instance of your subclass to sys.modules *first*, then call 
reload() on it to make the normal import process work.  PEAK uses this to 
implement lazy loading.

Actually, for your purposes, you might be able to just replace the module 
object and copy its contents to the new module's dictionary.



More information about the Python-Dev mailing list