[Python-Dev] Dynamic module namspaces

glyph at divmod.com glyph at divmod.com
Mon Jul 17 18:59:23 CEST 2006


On Mon, 17 Jul 2006 10:29:22 -0300, Johan Dahlin <jdahlin at async.com.br> wrote:

>I consider __getattribute__ a hack, being able to override __dict__ is less
>hackish, IMHO.

Why do you feel one is more "hackish" than the other?  In my experience the
opposite is true: certain C APIs expect __dict__ to be a "real" dictionary,
and if you monkey with it they won't call the overridden functions you expect,
whereas things accessing attributes will generally call through all the
appropriate Python-level APIs.

This makes sense to me for efficiency reasons and for clarity as well; if you're
trawling around in a module's __dict__ then you'd better be ready for what
you're going to get - *especially* if the module is actually a package.  Even in
"normal" python code, packages can have names which would be bound if they were
imported, but aren't yet.


More information about the Python-Dev mailing list