Significance of the _ init _. py file in the python package

Source: Internet
Author: User

1. In each package of the python module, There Is A _ init _. py file. Each Python package must contain this file. If _ init _. py does not exist, this directory is just a directory, rather than a package, and cannot be imported or contain other modules and nested packages.

2. A package is usually a directory except _ init __. in addition to the py file, there are some module files and subdirectories. If the subdirectory also contains _ init __. PY, It is the sub-package of this package.

3.The _ init _. py file will be executed when the package is imported.

4. The _ init _. py file defines the package attributes and methods. In fact, it can not be defined; it can be just an empty file, but it must exist. Another important variable in _ init _. py is called _ all __. Sometimes we make a trick to "import all", and import will register it in the package _ init __. import the sub-modules and sub-packages in the _ all _ list of The py file to the current scope.

Note that the __init _. py file is completely different from the _ init _ method. The _ init _ method automatically runs immediately when an object of the class is created. Therefore, the _ init _ method can be used to initialize your object.

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.