1 What is a module?
Common scenario: A module is a file that contains Python definitions and declarations, and the file name is the suffix of the module name plus the. Py.
In fact, the import loaded module is divided into four general categories:
1 code written using Python (. py file)
2 C or C + + extensions that have been compiled as shared libraries or DLLs
3 packages for a set of modules
4 built-in modules written and linked to the Python interpreter using C
2), module execution: Import, each module is a separate namespace.
A module can contain definitions of executable statements and functions that are intended to initialize modules that execute only when the module name is first encountered when importing an import statement (the import statement can be used anywhere in the program and is imported multiple times for the same module) , to prevent you from repeating the import, Python is optimized by loading the module name into memory after the first import, and the subsequent import statement only adds a reference to the module object that has loaded the large memory and does not re-execute the statements within the module.
3) 1,from: While the FROM statement is equivalent to import, a new namespace is created, but the name in the My_moudle is imported directly into the current namespace, and the name is used directly in the current namespace.
1 from My_moudle import read1,read2
Note: When you call the module, the same object, the name of the module, cannot appear in the page you are currently using. Otherwise there will be a cover situation, and when you overwrite later, you call the module will be permanently lost utility, so try not to take the same name as the module
PS: When you run a module, the module is loaded into memory, and you cannot change it at that time.
2.from Spam Import * Imports all names in spam that are not preceded by an underscore (_) into the current position.
In most cases our Python program should not use this type of import, because * you don't know what name you import,
It is likely that you will overwrite the name you have defined before. And the readability is extremely poor, there is no problem when importing in an interactive environment.
PS: from Import *: is to load all the contents of the spam into memory, and can be called at any time, and the location of loading is the current you enter this code location.
4)
For performance reasons, each module is imported only once, put into the dictionary sys.module, if you change the contents of the module, you must restart the program, Python does not support reloading or uninstalling the previously imported modules,
Some students may think of removing a module directly from Sys.module can not be uninstalled, note that you deleted the Sys.module module object may still be referenced by other program components, and therefore will not be cleared.
In particular, for the reference to a class in this module, a lot of objects are produced with this class, so these objects have references to this module.
Import Time,importlib Import AA Time.sleep (a) # importlib.reload (AA) aa.func1 () in 20 seconds of waiting time, modify the contents of the aa.py func1, waiting for the results of test.py. Open importlib comments, re-test
The result: it does not change, because when you run it, the system has loaded the code into memory and is not modified in memory.
If using reload, in the reload () is added to the name of the module is called, in addition to the change after the CTRL + S save, and then can be changed.
5): __name__
We can view the module name through the global variable __name__ of the module:
Run as script:
__name__ equals ' __main__ '
Import as module:
__name__= Module Name
Function: Used to control the. py file to perform different logic in different scenarios
if __name__ = = ' __main__ ':
__name__ : __name__ in the called module, the name of the called module is shown (for example: I am called the module name is VERW, then I call the print (__name__) display is Verw), and in the calling program is displayed __ main__.
6): When defining a variable name, the variable name should try not to repeat with the built-in function or module name, otherwise it will produce an error.
7): package: Meaning and precautions. (
relative paths can be used within the same package, specifically for others.
The absolute path is available in all places, but when one of the paths is changed all over again,
Therefore, the relative path is more used in the future.
Import *, to be received with ' __all__ ' = '
)
1. Whether it is an import or From...import form, it is the first time to be alert when you encounter a dot in an imported statement (rather than in use): This is the import syntax for packages
2. Packages are directory-level (folder-level), folders are used to form a py file (the essence of the package is a directory containing __init__.py files)
3. Import the file, the resulting namespace name from the file, import package, the name of the resulting namespace is also derived from the file, that is, the __init__.py under the package, the import package is the essence of the import of the file
Emphasize:
1. In Python3, even if there is no __init__.py file under the package, import package will still not error, and in Python2, the package must have the file, or import packet error
2. The purpose of creating the package is not to run, but to be imported to use, remember that the package is just a form of the module, the package is a module
Precautions : Import ... Either way, regardless of location, you must follow a principle when importing:
In the case of the import, the left side of the point must be a package, otherwise illegal. Can carry a series of points, such as Item.subitem.subsubitem, but all must follow this principle. 2. For import, there is no such restriction when used, the left side of the point can be a package, a module, a function, a class (they can all call their own properties in the way of a point). application scenario for import name: If we want to use name directly, the latter must be used.
2: Package Invocation-----Absolute Import and relative import
Our top package glance is for others to use, and then in the glance package will also have each other in the import of requirements, this time there are absolute import and relative import two ways: Absolute import: With glance as the starting relative import: with. Or. (can only be used in one package and not in different directories) For example: wewant to import glance/cmd/manage.py in glance/api/version.py
in Glance/api/version.py# Absolute Import fromimport Managemanage.main ()# relative import fromimport managemanage.main ()
Note: It is important to note that you can import the built-in or third-party modules with import (already in Sys.path), but to absolutely avoid importing the sub-modules of the custom package (not in Sys.path), you should use the From ... import ... Absolute or relative import, and the relative import of the package can only be used from the form.
PS: that is, a package of two different files, do not import directly, because a file import another B file, its C file will not call a.
(1) Absolute Import:
That is, from the import, another package is called from one package.
(2) Relative import:
That is, with. And.. Import. A point of time represents the sibling of the directory,. Two points of time represent the previous level of the directory.
PS: With. and. When represented, the same package cannot be called, even if the call is not used.
8): MySQL differs from Oracle
1. Oracle is a large database and MySQL is a small and medium-sized database, Oracle has a market share of only about 20% 40%,mysql, while MySQL is open source and Oracle is very expensive.
2. Oracle supports high concurrency, large traffic, and is the best tool for OLTP.
3. The space used to install the difference is also very large, MySQL installed after the 152M and Oracle has about 3G, and the use of Oracle occupies a particularly large memory space and other machine performance.
4.Oracle also some differences in MySQL operation
Modules and packages MySQL differs from Oracle