Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 513
Bunny Rabbit Avatar asked Jun 27 '26 21:06

Bunny Rabbit


1 Answers

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).

like image 121
Alex Martelli Avatar answered Jun 30 '26 12:06

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!