2

I stumbled over the import of __init__.py in python packages. See posts:

I was directed by Nate to very good explanations at this post:

Adding code to __init__.py.

I want to read more about this topic and more related topics. Could anyone suggest further formal documentations or books on the topics? TIA!

Community
  • 1
  • 1
HongboZhu
  • 4,442
  • 3
  • 27
  • 33

2 Answers2

2

Start here:

Python provides some acceptable tutorials which lead you to a basic understanding how the concepts of the language work.

This http://docs.python.org/tutorial/modules.html describes how modules and their initializers work.

In addition to that - if you want it the most formal way and can't get along with the language documentation for some reason - look in the PEPs (e.g. PEP 273 dealing with module imports from zip files)

Wizz
  • 1,267
  • 1
  • 13
  • 20
1

I found a very good explanation at:

http://effbot.org/zone/import-confusion.htm#what-does-python-do

and the critical sentence for me was:

When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.

HongboZhu
  • 4,442
  • 3
  • 27
  • 33