1、模块的定义:
一个.py文件就是一个模块.
模块文件中可以定义全局变量, 函数, 类.
2、导入模块的原因:
调用模块中的程序
3、导入模块的基本格式:
import 模块名
4、调用模块格式:
模块名.全局变量
模块名.函数(实参1,实参2,...)
模块名.类(实参1,实参2, ...)
【说明】这里的模块名就是模块文件的名字, 但是不包含.py
案例1:
import module01
import module02
print(module01.a)
print(module01.func1(5, 6))
s1 = module01.Student("robot", 20)
print(s1)
print("----------")
print(module02.a)
print(module02.func1(5, 6))
s2 = module01.Student("rabbit", 19)
print(s2)
-----------------
module01.py :
# 定义全局变量
a = 200
# 定义函数
def func1(a, b):
return a - b
# 定义类
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"name={self.name},age={self.age}"
---------------
module02.py :
# 定义全局变量
a = 200
# 定义函数
def func1(a, b):
return a - b
# 定义类
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"name={self.name},age={self.age}"

