Python的module之间相互调用

版权声明: https://blog.csdn.net/qq_37457432/article/details/87932115

Python的module之间相互调用,主要有三种常见用法

第一种是调用整个类,例如import numpy 所有numpy中的都被引入

第二种是调用类中特定的方法,例如from class_A import func_A 只调用class_A中特定的方法func_A

第三种,其实就是在import之后,为其弄个别名,例如import numpy as np ,在以后的numpy使用中,直接np.xxx即可

代码示例如下:

#counter.py 被调文件
def count(num_1,num_2):
    sum=num_1+num_2
    return sum

def ave(list):
    tot=0
    for i in list:
        tot+=i
    tot=tot/len(list)
    return tot

def pri(mess):
    print(mess)

def prierr():
    print("Error")

调用文件如下:

#Main.py 主调文件
import Counter as ct

ct.pri(ct.count(100,200))
ct.pri(ct.ave([100,200]))
ct.prierr()

运行结果如下:

300
150.0
Error

猜你喜欢

转载自blog.csdn.net/qq_37457432/article/details/87932115