- A中函数,B中调用A的函数
"""A.py中函数"""
def add(x,y):
return(x+y)
"""从A.py中调用函数"""
from mulu.A import add #mulu为包名
print(add(1,2))
- 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方法
本文详细介绍了如何在Python中从一个文件调用另一个文件中的函数和类,包括具体的代码示例,展示了如何导入和使用外部模块的功能。

被折叠的 条评论
为什么被折叠?



