How to understand the introduction of packages in Python

Python from import * and from import *, their functions are to introduce packages into use, but how do they execute and why use this syntax?

Import all functions from one module

from import * means means "I want to be able to access all the names that I have permission to access". For example, the following code something.py:

# something.py
public_variable =42
_ private_variable =141
def public_function():print("I'm a public function! yay!")
def _private_function():print("Ain't nobody accessing me from another module...usually")classPublicClass(object):
 pass
class_WeirdClass(object):
 pass

In the Python interpreter, we can execute from something import *, and then see the following:

from something import*
 public_variable
42
 _ private_variable
...
NameError: name '_private_variable' is not defined
 public_function()"I'm a public function! yay!"_private_function()...
NameError: name '_private_function' is not defined
 c =PublicClass()
 c
< something.publicclass object="" at=""...="" 
 c =_WeirdClass()...
NameError: name '_WeirdClass' is not defined

from something import *Imported all names from something except the names beginning with _. According to the specification, the names beginning with _ are private and therefore have not been imported.

What all is not mentioned above. all is a list of strings that specifies which symbols in the module (or package as mentioned later) will be exported when from import * is used. If we don't define all (we didn't define something.py above), import *The default import method is to import all names except the underscore (_). Again, the programming convention underlined indicates that a symbol is private and it is reasonable not to import it. Let's see what happens when we define our own all in something.py.

# something.py
__ all__ =['_private_variable','PublicClass']
# The rest is the same as before
public_variable =42
_ private_variable =141
def public_function():print("I'm a public function! yay!")
def _private_function():print("Ain't nobody accessing me from another module...usually")classPublicClass(object):
 pass
class_WeirdClass(object):
 pass

Now, we expect from something import * to only import _private_variable and PublicClass:

# something.py
__ all__ =['_private_variable','PublicClass']
# The rest is the same as before
public_variable =42
_ private_variable =141
def public_function():print("I'm a public function! yay!")
def _private_function():print("Ain't nobody accessing me from another module...usually")classPublicClass(object):
 pass
class_WeirdClass(object):
 pass

What is the bag?

When importing everything from a package, all is basically the same as a module, but it deals with the modules in the package (rather than importing all the names in the module). So when we use from import *. all means all modules that need to be imported into the current namespace.

The difference is that if you do not declare all in the init.py of a package, the from import * statement will not import anything (this statement is not entirely correct, here is the correct statement)

But what is wrong with this?

Before continuing, execute import this in your Python interpreter, and read the Zen of Python again (read it to your children every night before going to bed).

Being clear is better than being vague.

from import * is ambiguous. It doesn't tell us what we are importing or what we are bringing into the current namespace. A better approach is to explicitly import all the names we need. In this way, the reader (most likely yourself in the future) will not be confused about where a variable/method/class/other things used in your code come from. This also tells us the next point:

Readability is important

Even if you need to import many things, it is clearer to import them one by one explicitly. Use PEP 328:

from Tkinter import(Tk, Frame, Button, Entry, Canvas, Text,
 LEFT, DISABLED, NORMAL, RIDGE, END)

You can now clearly know what is in your namespace, and using ctrl+f can quickly tell you where they come from.

At the same time, you always have to bear the risk of the module/package author changing the content of the list (adding/subtracting things).

Content expansion:

Basic note

The above is how to understand the details of the introduction of packages in Python. For more detailed information about the introduction of packages in Python, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

How to understand the introduction of packages in Python
How to understand a list of numbers in python
How to understand variables in Python
How to find the area of a circle in python
How to understand global variables in Python
How to use the zip function in Python
An article to understand the yield in Python
How to use the format function in python
How to install the downloaded module in python
Introduction to the use of Hanlp in ubuntu
How to understand python objects
Example of how to automatically download pictures in python
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to view the python module
How to clear variables in python
How to understand python object-oriented programming
The usage of tuples in python
How to use SQLite in Python
Understanding the meaning of rb in python
How to verify successful installation of python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
The usage of Ajax in Python3 crawler
How to save text files in python
How to install JDK 13 in the Linux environment using compressed packages
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
01. Introduction to Python
How to define private attributes in Python
How to add custom modules in Python
How to view installed modules in python
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
Introduction to Python
How to add background music in python
How to represent relative path in python
What is the function of adb in python
How to program based on interfaces in Python
How to simulate gravity in a Python game
The meaning and usage of lists in python
How to open the ubuntu system in win10
How to use code running assistant in python
How to enter python through the command line
How to set code auto prompt in python
How about learning python at the age of 27?
How to switch the hosts file using python
Teach you how to write games in python
How to delete files and directories in python
The consequences of uninstalling python in ubuntu, very
How to write a confession program in python