2 回答
TA贡献1812条经验 获得超5个赞
SignalsAdd.create_signal(...
SignalsAdd是一个类,而不是该类的实例。当尝试以这种方式调用该方法时,它被视为普通函数;因此字符串"crypto"成为self(而不是signalType)的值,等等。一个更简单的例子:
>>> class x:
... def func(self, y):
... print('self:', self, 'y:', y)
...
>>> x.func(2) # wrong
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() missing 1 required positional argument: 'y'
>>> x.func(1, 2) # trying to fix it, but not really proper
self: 1 y: 2
>>> x().func(2) # create an x instance, and use its method
self: <__main__.x object at 0x0000027AF1057320> y: 2
或者,可以在没有实例的情况下调用的东西@classmethod(类将作为第一个参数而不是实例传递;这仍然可以让您更改子类的行为):
>>> class x:
... @classmethod
... def func(cls, y): # we rename the argument for clarity.
... print('class:', cls, 'y:', y)
...
>>> x.func(2) # It works with either the class...
class: <class '__main__.x'> y: 2
>>>
>>> x().func(2) # or an instance; the instance's class is looked up.
class: <class '__main__.x'> y: 2
或者 as @staticmethod(什么都没有传递,它只是将一个普通函数放入类的命名空间):
>>> class x:
... @staticmethod
... def func(y):
... print('y:', y)
...
>>> x.func(2) # it also works either way, but there is no extra value
y: 2
>>> x().func(2) # passed automatically, so no way to check the subclass.
y: 2
但是,很有可能您一开始并不真正想要上课。这不是Java;顶层的普通函数工作得很好,并且通常是完成这项工作的最佳工具。
TA贡献2036条经验 获得超8个赞
我认为明确传递 self 可能会导致问题。在调用函数时,尝试使用变量名传递值,看看你得到了什么。在这里,您至少会知道哪个值传递给哪个变量以及缺少什么。
像这样
add_signal = SignalsAdd.create_signal(signal_type="crypto", currencyfrom="5c1e4633b140f7000f908897", ...)
添加回答
举报
