Functions of the _ init _. py file in python

Source: Internet
Author: User

Each module package in python contains a _ init _. py file. With this file, we can import the module under this directory.
So, __init _. py, what other functions are there?
Actually, __init _. py still contains content. When we import a package, we actually import its _ init _. py file.
We can import other packages or modules in the _ init _. py file.
[Python]
Import readers
Import writers
Import commands
Import users
Import meta
Import auth
Import admin

In this way, when we import this package, the __init _. py file runs automatically. After importing so many modules, we do not need to write all the import statements in one file, which can reduce the amount of code.
You do not need to import modules one by one.
Another important variable in _ init _. py is called _ all __. We sometimes make a trick to "import all", that is:
From PackageName import *
In this case, import the sub-modules and sub-packages registered in the _ init _. py file _ all _ list to the current scope. For example:
# File _ init _. py

_ All _ = ["Module1", "Module2", "subPackage1", "subPackage2"]


Author: Chen jianhong

Related Article

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.