Python 模块导入

#第一种模块导入方式
import send

#调用send中的函数,类
msg1 = send.Msg()
msg1.send()

send.test()
#模块第二种导入

#从send模块中导入test函数
from send import test

#这种方式导入可以不用加模块名
test()
#第二种导入模式的风险

#*表示导入send模块里所有的类或者函数
from send import *

from test import *


#调用test()函数,test()函数在两个模块中都有,就会发生覆盖

#这里会执行test.py中的test()函数

test()
class Msg(object):
        def send(self):
                print("send message .")



def test():
        print("i am test from send.py")

#python使用import导入模块,python解析器会先将模块执行一遍,
#如果不想模块中的某些方法被执行,需要使用__name__内置变量
#__name__内置变量,当直接执行本模块时,__name__的值是"__main__"
#当本模块被其它python文件导入时,__name__的值是本模块的名字
#因此可以通过__name__变量来屏蔽某些不想被其他Python文件调用的方法

if "__main__" == __name__:
        test()
else:
        print("send.py 正在被调用.")
原文地址:https://www.cnblogs.com/zhanggaofeng/p/9692699.html