[Python] basis of the import and use of statements from

Unexpectedly, the first article in November contributed to the use of Python --import basic grammar and sentence from the statement.
Getting started when Python did not care so much detail, so for a long time to import and from statements use is scanty, when used to come in handy. He gradually began to write more code, we inadvertently will care about the details of the provisions of this article all the major record using a format import and from statements.

import

For syntax only import mode, there are the following two formats:
import module [as alias]

import the package. [... package] Module [as alias]

You can see, the end of the import are "modules" name, which means that the imported module using the import method in use module, class or variable must add the module name in front of their name, for example:

import time
time.time()

from … import …

For the way from ... import ... There are three formats:
from bag [package ...] import module [as alias].

from the package. import module Method

from module import method

Package is a folder, the location of this folder may be PYTHON_PATH subfolders, it is also possible in the current sub-py files folder where the folder or subfolder under the folder path manually added.

We were introduced:
from package [package ...] import module [as alias].
You can see, from behind with a "package name", import followed by a "module", so when in use, but also need to "modular approach. / class / variable "format to call.
from the package. Module import method [as alias]
this on and above that is not the same, from last with a "module", while import followed by a method name, so that it can be directly invoked by the method name, You do not need to add the package in front of the name, because the method of a module has been specified.
Note: Import from ... import * using this method, when all the module can not be used [as alias] format. Import introduced using a particular method, or class, as may be used to rename. example:

from pk1.pk2.pk3.module import func as new_func
new_func()

from module import method [as alias]
a and this Similarly, when calling the method, no need to put the package name. example:

from time import time
time()
Published 61 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41427568/article/details/102976098