What are the ways to import objects in modules in python?

coldplay.xixi
Release: 2023-01-03 09:25:02
Original
14239 people have browsed it

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.

What are the ways to import objects in modules in python?

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[,...]]
Copy after login

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
Copy after login

2. Use it with form to import the entire module:

General format For:

form
模块名
import*
Copy after login

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
Copy after login

3. Use with form to import one or more objects:

The general format is:

form
模块名
import
对象1[,对象2[,对象...]]
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template