Python TypeError:generator()缺少1个必需的位置参数:';json';
错误: 我的代码:Python TypeError:generator()缺少1个必需的位置参数:';json';,python,python-3.x,arguments,Python,Python 3.x,Arguments,错误: 我的代码: Traceback (most recent call last): File "...", line ..., in main output_call = output.generator(json_text) TypeError: generator() missing 1 required positional argument: 'json' 我的for循环是迭代通过字典生成的类列表 class TOTALTIME: def generator(
Traceback (most recent call last):
File "...", line ..., in main
output_call = output.generator(json_text)
TypeError: generator() missing 1 required positional argument: 'json'
我的for循环是迭代通过字典生成的类列表
class TOTALTIME:
def generator(self, json):
self._json = json
print('{} minutes'.format(int(self._json['route']['time']/60)))
下面是创建类的函数:
for output in output_list:
output_call = output.generator(json_text)
print(output_call)
您正在调用未绑定函数,而不是绑定方法。您需要为类上的函数创建实例以绑定到某个对象,并提供self
参数
换句话说,您的output_列表
包含TOTALTIME
条目,而不是TOTALTIME()
条目:
def list_of_outputs(n_outputs: int) -> 'values':
d = {}
e = {'STEPS': output_classes.STEPS,
'LATLONG': output_classes.LATLONG,
'TOTALTIME': output_classes.TOTALTIME,
'TOTALDISTANCE': output_classes.TOTALDISTANCE,
'ELEVATION': output_classes.ELEVATION }
for output in range(n_outputs):
add_output = input()
if add_output in e.keys():
d.update(e)
return d.values()
>类总时间:
... def生成器(self,json):
... self._json=json
... 打印({}分钟)。格式(int(self._json['route']['time']/60)))
...
>>>生成器({'route':{'time':360})
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:generator()缺少1个必需的位置参数:“json”
>>>TOTALTIME().generator({'route':{'time':360})
6分钟
请注意创建实例的
()
调用。output
似乎是类,而不是类的实例。output\u list
由TOTALTIME
或TOTALTIME()
组成吗?我的output\u list由TOTALTIME等组成。如果我的字典像{'key':TOTALTIME()}一样,它会给我一个错误
>>> class TOTALTIME:
... def generator(self, json):
... self._json = json
... print('{} minutes'.format(int(self._json['route']['time']/60)))
...
>>> TOTALTIME.generator({'route': {'time': 360}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: generator() missing 1 required positional argument: 'json'
>>> TOTALTIME().generator({'route': {'time': 360}})
6 minutes