< turn >python: __init__.py usage

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/BeginMan/p/3183629.html

Python has a __init__.py file in each of the module's packages, and with this file we can import the module in this directory.
So, what other functions does __init__.py have?
In fact, __init__.py inside can still have content, we import a package, actually imported its __init__.py file.
We can then import other packages, or modules, into the __init__.py file.
[Python]
Import Readers
Import writers
Import commands
Import Users
Import Meta
Import Auth
Import Admin

This way, when we import this package, the __init__.py file runs automatically. To help us import so many modules, we do not need to write all the import statements in a file, but also reduce the amount of code.
There is no need to import the module.
There is also an important variable in __init__.py, called __all__. We sometimes take a trick of "import All", which is this:
From PackageName Import *
Import then imports the sub-modules and sub-packages that are registered in the __ALL__ list in the package __init__.py file into the current scope. Like what:
#文件 __init__.py

__all__ = ["Module1", "Module2", "SubPackage1", "SubPackage2"]

such as: In a bag with foo.py, __init__.py

#__init__.pyimport osimport datetime   
#foo.pyimport *print Datetime.datetime.now ()  

Then output: 2013-07-11 11:34:41.250000

< turn >python: __init__.py usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.