我正在编写一个 python 程序,它应该从互联网上获取图像。因此我创建了一个主控制器和一个 APIService。因为我想让控制器更轻便,所以我在 APIServices 中添加了一些功能。
不幸的是,我无法调用 APIService 类中的其他函数,我总是收到错误消息:(“name 'fetch' is not defined”)。
有没有办法在类中调用方法,或者在 python 中不支持这种方法?
代码示例:
class APIService(object):
def __init__(self, url):
#init
def fetch(url):
#fetch Image
def fetchLogic(self, url):
for i in url:
fetch(i) #here the error accures
class Controller(object):
def __init__(self):
#init
def callAPI()
api = APIService("url")
api.fetchLogic([url1,url2])
if __name__ == "__main__":
Controller()