12.4 导入模块
12.4.1 语句
[root@node01 20180508]# cat demo.py
version =10
class demo(object):
def fun1(self,a,b):
return a+b
[root@node01 20180508]# cat a1.py
from demo import *
a=demo()
print a
print type(a)
print a.fun1(44,66)
[root@node01 20180508]# python a1.py
<demo.demo object at 0x7fcb3aeffb50>
<class 'demo.demo'>
110
12.4.2 from-import 语句:
你可以在你的模块里导入指定的模块属性,也就是把指定名称导入到当前作用域。
[root@node01 20180508]# cat a1.py
from demo import version
print version
a=demo()
print a
print type(a)
print a.fun1(44,66)
[root@node01 20180508]# python a1.py
10
Traceback (most recent call last):
File "a1.py", line 3, in <module>
a=demo()
NameError: name 'demo' is not defined
[root@node01 20180508]#
只导入了version
12.4.4 扩展的import 语句(as)
有时候你导入的模块或是模块属性名称已经在你的程序中使用了,
[root@node01 20180508]# cat a1.py
import demo
print demo.version
a=demo.demo()
print a
print type(a)
print a.fun1(44,66)
[root@node01 20180508]# python a1.py
10
<demo.demo object at 0x7fae92850b50>
<class 'demo.demo'>
110
import demo as scan
print scan.version
a=scan.demo()
print a
print type(a)
print a.fun1(44,66)
[root@node01 20180508]#
[root@node01 20180508]# python a1.py
10
<demo.demo object at 0x7f8899d1ab50>
<class 'demo.demo'>
110