Sam*_*Sam -2 python python-2.7
假设有一个名为的文件
test.py包含:
class calc:
def add (a,b):
add = a + b
print add
def sub (a,b):
sub = a-b
print sub
Run Code Online (Sandbox Code Playgroud)
如果我想在另一个文件中调用add或sub,请说ex1.py
我试过了
from test import calc
t = calc()
t.add(5,4)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
如何在另一个文件中调用add或sub?
如果test没有class但是函数我知道我们可以通过test.add调用(4,5)
添加self到参数列表,因为它会自动传递给类方法:
class calc:
def add (self, a,b):
add = a + b
print add
def sub (self, a,b):
sub = a-b
print sub
Run Code Online (Sandbox Code Playgroud)