Original link: https://chegva.com/5362.html
◎Knowledge points
-
Overview of modules
-
Use modules from the standard library
-
How to import modules
◎Script practice
▽ Module overview
"""I. Overview of the module""" """ 1. What is a module? A module is a .py file of python. Variables, functions, and classes can be defined in a module. The variables, functions, and classes defined in a module are collectively referred to as module attributes. 2. Why use modules? (1) Code reuse In one module, another module can be imported to reuse attributes defined in another module (2) Avoid attribute name conflicts Attributes with the same name can exist in different modules 3. How to organize and manage modules In order to better organize and manage modules, python introduces packages. After adding a module __init__.py to a directory, the directory becomes a package. So a package is a special directory that contains a specific module. The role of the module __init__.py is to initialize the package it is in. If initialization is not required, its content can be empty. The directory supports nesting, so the package also supports nesting, and the package can also have sub-packages """
[zkqw]
▽ Using modules from the standard library
"""Second, use the modules in the standard library""" """ Python officially provides us with a standard library, which has a lot of modules for us to use to complete various tasks. The path of the standard library in the MacOS system: /Library/Frameworks/Python.framework/Versions/3.x/lib/python3.x The path of the standard library in Windows system: <python installation path>\Lib The source code for the modules in the standard library is excellent python learning material. If you want to use a module from the standard library, you must use the import statement to import it. There are two ways to import (1) Import the entire module (2) Import the attribute """ in the module """ 1. Import the entire module The syntax for importing the entire module is: import [package name.] module name If the imported module is in a package structure, it must be navigated to the module through all its parent packages: Top-level parent package name.Subpackage name....Subpackage name After importing the entire module, you can access the attributes in the module (including: variables, functions and classes), the syntax format is: [packagename.]modulename.propertyname When you enter "[package name.]module name.", in the list of code prompts, variables are represented by yellow v(variable), Functions are represented by pink f (function), and classes are represented by blue c (class) """ # import os # All environment variables in the operating system # print(os.environ) # A specific environment variable in the operating system # print(os.getenv('PYTHON_HOME')) # MutableMapping class # print(os.MutableMapping) # import xml.dom.minidom # print(xml.dom.minidom.StringTypes) """ When importing an entire module, you can give the imported module an alias. Its syntax is: import [package name.] module name as module alias """ import os as operating_system # All environment variables in the operating system # print(operating_system.environ) # A specific environment variable in the operating system # print(operating_system.getenv('PYTHON_HOME')) # MutableMapping class # print(operating_system.MutableMapping) # import xml.dom.minidom as md # print(md. StringTypes) """ 2. Attributes in the import module The syntax format of an attribute in the import module is: from [package name.] module name import attribute name Similarly, if the imported module is in a package structure, it must pass all its The parent package navigates to the module: Top-level parent package name.Subpackage name....Subpackage name After importing the properties in the module, you can directly access the properties in the module without adding the prefix "[package_name.]module_name", This makes the code more concise, but the code is less readable than adding the prefix """ # from os import environ # print(environ) # from os import getenv # print(getenv('PYTHON_HOME')) # from os import MutableMapping # print(MutableMapping) # from xml.dom.minidom import StringTypes # print(StringTypes) """ The syntax format of importing multiple attributes in a module is: from [package name.]module name import attribute name 1, attribute name 2, ..., attribute name n """ # from os import environ, getenv, MutableMapping # print(environ) # print(getenv('PYTHON_HOME')) # print(MutableMapping) """ When importing a property in a module, you can assign an alias to the imported property. Its syntax format is: from [package name.]module name import attribute name 1 as attribute name 1 alias, attribute name 2 as attribute name 2 alias, ..., attribute name n as alias of attribute name n """ # from os import environ as er, getenv as ge, MutableMapping as MM # print(er) # print(ge('PYTHON_HOME')) # print(MM) """ The attributes in the module can be imported all at once, and the syntax format is: from [package name.]module name import * This type of import is strongly discouraged because: (1) Low efficiency (all attributes are imported) (2) The readability of the code is poor (do not know which properties are imported) (3) Error prone (when the same attribute exists in two modules) """ from os import * print(environ) print(getenv('PYTHON_HOME')) # print(MutableMapping) """ When importing an entire module, if the module is in a package structure, you can also use a syntax similar to importing attributes in a module: from package name import module name """ from xml.dom import minidom print(minidom.StringTypes)
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/module.py
[/zkqw]
This article is reprinted from: https://chegva.com/5362.html
This site is for inclusion only, and the copyright belongs to the original author.