Does 'import *' Import __init__.py in Python?
What Does "import *" Import in Python and How Does It Relate to __init__.py?
In Python, the "import *" statement imports all non-private (i.e., not starting with an underscore) symbols from a specified module into the current module. This allows direct access to the imported symbols without using the module name prefix.
Does "import *" Import __init__.py?
No, the "import *" statement does not automatically import the __init__.py file found in the containing folder. __init__.py is a special file that Python uses to initialize a module when it is imported. To explicitly import __init__.py, you must use the "import" statement with the module name as follows:
Advantages and Disadvantages of "import *":
The main advantage of "import *" is the convenience of importing all symbols at once. However, this practice is generally discouraged due to:
- Namespace collisions: Imported symbols may conflict with existing symbols in the current module.
- Inefficiency: Importing a large number of symbols may slow down the program.
- Lack of explicit documentation: It can be difficult to track the origin of imported symbols when using "*".
Preferred Practices:
Instead of "import *", it is recommended to import only the specific symbols you need. This can be done using either:
Explicit import:
Module aliasing:
Submodules and "import *":
When a module contains submodules, "import *" does not import these submodules unless they are explicitly included in the all attribute of the main module.
Conclusion:
While "import *" can be convenient, it is generally discouraged in favor of explicit imports or module aliasing. __init__.py is a special file used to initialize a module, and it must be imported separately using the "import" statement.
The above is the detailed content of Does 'import *' Import __init__.py in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
