基础DAY11-模块import
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import hm_01_测试模块1 import hm_02_测试模块2 hm_01_测试模块1.say_hello() dog = hm_01_测试模块1.Dog() print(dog) hm_02_测试模块2.say_hello() cat = hm_02_测试模块2.Cat() print(cat) print(hm_01_测试模块1.title) print(hm_02_测试模块2.title)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import hm_01_测试模块1 as DogModule import hm_02_测试模块2 as CatModule DogModule.say_hello() dog = DogModule.Dog() print(dog) CatModule.say_hello() cat = CatModule.Cat() print(cat) print(DogModule.title) print(CatModule.title)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from hm_01_测试模块1 import Dog from hm_02_测试模块2 import Cat # 如果两个模块,存在同名的函数,那么导入模块的函数,会覆盖掉先导入的函数 from hm_02_测试模块2 import say_hello from hm_01_测试模块1 import say_hello # 不需要使用模块调用类了 dog = Dog() print(dog) cat = Cat() print(cat) say_hello()
from hm_01_测试模块1 import Dog
from hm_02_测试模块2 import Cat
# 如果两个模块,存在同名的函数,那么导入模块的函数,会覆盖掉先导入的函数
# 统一写在顶部,可以使用别名as分别定义同名的函数
from hm_02_测试模块2 import say_hello as say_hello1
from hm_01_测试模块1 import say_hello as say_hello2
# 不需要使用模块调用类了
dog = Dog()
print(dog)
say_hello1()
say_hello2()
from import *(不建议使用)
函数重名没有任何的提示,出现问题不好排查
import random
rand = random.randint(1, 10)
print(random.__file__)
print(rand)
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\random.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 全局变量 函数 类,直接执行的代码不是向外界提供的工具 def say_hello(): print("大家好,我是say hello") # 如果直接执行模块 得到的值永远是__main__ if __name__ == "__main__": # 文件被导入时,能够直接执行的代码不需要被执行 print(__name__) print("这是小明开发的模块") say_hello()