Python中不同文件下函数与类之间的调用

  1. A中函数,B中调用A的函数
"""A.py中函数"""
def add(x,y):
    return(x+y)

"""从A.py中调用函数"""
from mulu.A import add #mulu为包名
print(add(1,2))
  1. A中是类,B中调用A的类
"""A.py中的类"""
class A:
    def __init__(self, num1, num2):
        self.x = num1
        self.y = num2

    def add(self):
        return ("x和y的和为:%d" % (self.x + self.y))
"""从A.py中调用类"""
from mulu.A import A #mulu为包名

a=A(2,3) #实例化并传参

print(a.add()) # 调用实例化a中的add方法

猜你喜欢

转载自blog.csdn.net/Python_BT/article/details/108266306