开发者

python modules appearing out of no where

Today one peculiar thing happened to me .I was trying to get a hang of appengine and Django on www.shell.appspot.com when i entered dir(django) the o/p i got was

['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__path__', 'conf', 'core', 'template', 'utils']

but still i tried

from django import forms

and it worked to my surprise , while there was no trances of this on the o/p of dir().so out of curiosity i again entered dir(django) and the o/p i got was

['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__path__', 'conf', 'core', 'forms', 'oldforms', 'template', 'utils']

note the forms element here .So can 开发者_开发问答any one explain to me where this forms come from ?


The statement from package import module loads (if it had not been previously loaded) package/module.py (after first loading package/__init__.py if it hadn't previously loaded it already) and adds 'module' as an entry in the package (as well as a variable in the current scope). So dir(package) will show a 'module' entry after the import, but not before.

A package can contain unbounded numbers of modules and subpackages (recursively) so it would be very slow to load everything in the package (just to fill out its dir!-) until specific modules and subpackages are specifically imported -- so, the loading of the latter is "just in time", when they're imported for the first time (and only then do they show up in the paren package's dir).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜