Python-模块随记.md 1.6 KB

这是主程序main.py# 请阅读代码注释

import test # 导入test模块
print(test.a) # 使用“模块.变量”调用模块中的变量
test.hi() # 使用“模块.函数()”调用模块中的函数
A = test.Go2() # 使用“变量 = 模块.类()”实例化模块中的类print(A.a) # 实例化后,不再需要“模块.”A.do2() # 实例化后,不再需要“模块.”

==import test==

==print(test.a)==

==test.hi()==

==A = test.Go2()==
==print(A.a)==
==A.do2()==

==a = '====我是模块中的变量====a'==

==def hi():==
==a = '====我是函数里的变量====a'==
==print('====函数====“hi”====已经运行!====')==

==class Go2:==
==a = '====我是类====2====中的变量====a'==
==def do2(self):==
==print('====函数====“do2”====已经运行!====')==

==from test import hi #== ==从模块====test====中导入函数====“hi”==
==hi() #== ==使用函数====“hi”====时无需加上====“====模块====.”====前缀==

==def hi():==
==print('====函数====“hi”====已经运行!====')==

==a = '====我是模块中的变量====a'==

==def hi():==
==a = '====我是函数里的变量====a'==
==print('====函数====“hi”====已经运行!====')==

==class Go2:==
==a = '====我是类====2====中的变量====a'==
==def do2(self):==
==print('====函数====“do2”====已经运行!====')==

==from test import a,hi,Go2==

==print(a) #== ==打印变量====“a”==

==hi() #== ==调用函数====“hi”==

==A = Go2() #== ==实例化====“Go2”====类==
==print(A.a) #== ==打印实例属性====“a”==
==A.do2() #== ==调用实例方法====“do2”==