Home>Article>Backend Development> What are the ways to import objects in modules in python?
How to import objects in modules in python: 1. Import the entire module, the format is [import module 1[module 2[,...]]]; 2. Use it with form to import the entire module, The format is [form module name import*]; 3. Use it with form to import one or more objects.
The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer. This method is suitable for all brands of computers.
How to import objects in a module in python:
1. Import the entire module:
The general format is:
import 模块1[模块2[,...]]
The module name is the prefix of the program file, excluding.py
. Multiple modules can be imported at one time. After importing the module, when calling functions or classes in the module, you need to use the module name as a prefix, so that the code is easier to read and understand.
Example:
>>>import math >>>math.sin(0.5) 0.4794255
2. Use it with form to import the entire module:
General format For:
form 模块名 import*
After importing the module in this way, call the function or class in the module, using only the function name or class name. The code is concise, but the readability is poor and it is not easy to understand
Example :
>>>form math import* >>>cos(0.5) 0.8775825
3. Use with form to import one or more objects:
The general format is:
form 模块名 import 对象1[,对象2[,对象...]]
This method only imports modules One or more objects in the module, when calling objects in the module, only use the object name (#Similar to method two)
Example:
>>>form math import sin,cos,exp >>>sin(0.5) 0.4794255 >>>cos(0.5) 0.8775825 >>>exp(1) 2.7182818
Related free learning Recommended:python video tutorial
The above is the detailed content of What are the ways to import objects in modules in python?. For more information, please follow other related articles on the PHP Chinese website!