Home > Backend Development > Python Tutorial > Why Should You Avoid `import *` in Python?

Why Should You Avoid `import *` in Python?

Susan Sarandon
Release: 2024-11-15 19:26:02
Original
582 people have browsed it

Why Should You Avoid `import *` in Python?

Uncovering the Secrets of Import * in Python

In Python, the enigmatic command "import *" grants unprecedented access to a module's resources. It imports not just the designated module but also the cherished __init__.py file residing within the containing folder. As a result, the blessed __init__.py module becomes integral to the current namespace.

Declaring from project.model

With the power of import , the need to explicitly declare "from project.model import __init__" evaporates. A simple "from project.model import " suffices to embrace everything within both the project.model module and its hallowed __init__.py file.

Pros and Cons of Import *

The allure of import * lies in its ability to import all named objects (barring those beginning with an underscore) from a designated module into the current namespace. This allows hassle-free access to any imported object without the burden of prefixing its name with the module's identifier.

For instance, consider the following snippet:

from math import *
Copy after login
Copy after login

This magical incantation allows us to seamlessly wield the power of pi without any prior acquaintanceships:

pi
3.141592653589793
Copy after login

However, the immense power of import * comes at a price. It brings forth the perils of namespace collisions, potentially overwriting existing variables within the current scope. Additionally, it may prove inefficient if the target module harbors a plethora of objects. Lastly, and perhaps most importantly, it stifles the self-documenting nature of code, leaving the origin of variables, methods, and classes shrouded in mystery.

A Cleaner Approach

Hence, the wise among us reserve import * for ad-hoc testing and prefer more explicit alternatives for production code. One prudent choice is to explicitly import only the desired objects:

from math import pi
Copy after login

Alternatively, importing the entire module under a distinct namespace can be a sensible compromise:

import math as m
Copy after login

What Lurks Within Import *

To unravel the mysteries of import *, it is imperative to understand its workings. By default, it imports all named objects (excluding those beginning with an underscore). However, should the designated module define an all variable, precedence is given, and only the names enumerated within that variable are imported.

Sub-modules and Import *

Many standard Python libraries are organized into sub-modules, such as urllib.request, the ubiquitous extension of the urllib module. It is important to note that import * from urllib will not traverse into these hallowed halls; separate import statements are required to access them:

from math import *
Copy after login
Copy after login

Conclusion

The humble import * commands wield immense power, offering the ability to import an entire module's contents into the current namespace. However, it is a power that must be wielded with caution, lest the perils of namespace collisions, inefficiency, and lack of self-documentation ensnare us. For production code, more explicit and structured import practices are the path to tranquility and clarity.

The above is the detailed content of Why Should You Avoid `import *` in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template