python导包学习总结

python初学者,对于导包纠结了不少时间,总结分享,持续前进~

Python导包的两种方法:

  1.1  from 包.模块  import 方法名,调用时直接使用方法名()

  1.2  from 包.模块  import 类名,调用时直接使用类名().方法名(),即通过对象直接去调用方法

  2.1  import  包.模块名   调用时要加绝对路径,包.模块.方法名()

  2.2  import  包.模块名   调用时要加绝对路径,包.模块.类名().方法名()

test下有function1 function2中为方法,class_1和class_2中是类定义,在function1中分别调用function2和class_1中的方法:

#import导包
import test.function_2
import test.class_1
def func_1():
print("function 1")

  test.function_2.func_2()
    test.class_1.test_class_1().class_func_1()

#from  import导包
from test.function_2 import func_2
from test.class_1 import class_func_1
def func_1():
print("function 1")

  func_2()
   test_class_1().class_func_1()
如果类之间要互相调用方法,也可以继承
from test.class_2 import test_class_2  #导包
class test_class_1(test_class_2): #继承
def class_func_1(self):
print("function of class 1")
test_class_2().class_func_2() #对象.方法 

 

posted @ 2018-06-07 20:10  fr_up  Views(501)  Comments(0Edit  收藏  举报