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", ...)
添加回答
舉報