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