November 30, 2008

python: directly refering __init__.py variables when importing * from a package

From this python doc page: import * from a package
We know that submodules of a package won’t be imported in a “import * from …” statement unless you define the all list in init.py .
However, they didn’t say that after you have defined all list in init.py, everything you can refer to directly is restricted to the names in that list, and other public names defined in init.py won’t be directly accessible anymore…


I guess this is how it works:
The public names imported in “import * from …” statement come from all list.
When all is not defined, the system would automatically put all the public names in init.py into all list. When all is defined, the system would just use the user’s list and won’t bother to add anything else.

One more point about init.py: the public names in it won’t be directly accessible to the submodules in this package unless you explictly import this package in those modules. This is especially confusing when people are used to object oriented packaging scheme.

Comments »

The URI to TrackBack this entry is: http://songwei.blogsome.com/2008/11/30/python-directly-refering-__init__py-variables-when-importing-from-a-package/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>