python中的import模块引用(二)

版权声明:此博客内容为本人学习笔记,转载请标明出处!本人所有文章仅发布于CSDN! https://blog.csdn.net/weixin_40457797/article/details/83113178

我们可以使用from import来将指定模块里所有变量(包含变量名)导入进来 

#继续采用上面的路径#
from first.first_1.a1 import *
print (a + b * c - d + e * f)

如果不想引用模块的所有变量,可以直接在模块中设置__all__ =,或者直接引入需要的变量

#__all__ =方法#
#路径为first\first_1\a1.py的a1模块内:#
__all__ = ('a','b','c')
a = 11
b = 22
c = 33
d = 44
e = 55
f = 66

#路径为first\first_2\a2.py的a2模块内(在a2模块内调用a1模块):#
from first.first_1.a1 import *
print (a)
print (b)
print (c)
print (d)
print (e)
print (f)
#最后结果只会打印出a、b、c#




#直接引入需要的变量方法#
#路径为first\first_1\a1.py的a1模块内:#
a = 11
b = 22
c = 33
d = 44
e = 55
f = 66

#路径为first\first_2\a2.py的a2模块内(在a2模块内调用a1模块):#
from first.first_1.a1 import a,b,c
print (a)
print (b)
print (c)
print (d)
print (e)
print (f)
#最后结果和上面一样#


#在使用第二种方法时如果输入过多,需要换行来避免影响阅读,可以使用括号#
#路径为first\first_1\a1.py的a1模块内:#
a = 11
b = 22
c = 33
d = 44
e = 55
f = 66

#路径为first\first_2\a2.py的a2模块内(在a2模块内调用a1模块):#
from first.first_1.a1 import (a,b,
c)
print (a)
print (b)
print (c)
print (d)
print (e)
print (f)
#最后结果和上面一样#

猜你喜欢

转载自blog.csdn.net/weixin_40457797/article/details/83113178
今日推荐