python 函数导入和方法导入

12.7.2   使用from-import 导入包:

函数导入:

[root@node01 20180509]# cat tt1.py 
def fun1():
  return 1
def fun2():
  return 2
Str='aaa123'

[root@node01 20180509]# cat tt1.py
def fun1():
  return 1
def fun2():
  return 2
Str='aaa123'
[root@node01 20180509]# cat a1.py 
from tt1 import *
print Str
[root@node01 20180509]# python a1.py
aaa123


[root@node01 20180509]# cat a1.py 
from tt1 import *
print Str
print fun1()
print fun2()
[root@node01 20180509]# python a1.py
aaa123
1
2

方法导入:


[root@node01 20180509]# cat tt2.py
class tt2(object):
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def fun1(self,x,y):
        return x+y
    def fun2(self,x,y):
         return x+y
[root@node01 20180509]# cat a2.py 
from tt2 import *
a=tt2('11','22')
print a
print type(a)
print a.fun1(3,5)
print a.fun2(9,5)
[root@node01 20180509]# python a2.py 
<tt2.tt2 object at 0x7fc053755c10>
<class 'tt2.tt2'>
8
14

原文地址:https://www.cnblogs.com/hzcya1995/p/13349219.html