亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

發生“缺少一個位置參數”錯誤,但所有參數都存在

發生“缺少一個位置參數”錯誤,但所有參數都存在

呼如林 2022-06-28 10:00:59
原始代碼有效:import requestsfrom api.signals_add import SignalsAddclass TestLogin:    def test_log_in(self):        url = 'https://api.stg.nuroblock.com/api/admin/auth/login'        data = {"email": "[email protected]", "password": "123123"}        r = requests.post(url, json=data)        assert 200 == r.status_code    def test_create_signal(self):        json_for_create_signal = {                "signalType": "crypto",                "currencyFrom": "5c1e4633b140f7000f908897",                "currencyTo": "5c1e4633b140f7000f908898",                "currencyPair": "5cbd7faf496a8c001124ed5b",                "type": "sell",                "buyTip": {"value": 21313},                "stopTip": 21312.9999,                "stopTipPips": "-1",                "takeProfits": [{"value": 21313.0111, "isAchieved": False, "closeOnReach": False, "pips": "+111"}],                "status": "active",                "orderType": "market"        }        result = requests.post("https://api.stg.nuroblock.com/api/admin/signals",                               json=json_for_create_signal,                               headers={'Authorization': 'Bearer ea3d14e631683062073186622d58d2a16HSGB9JDC0ZYtELpwEGL8eNma36EdXei/B72NOa5Y5ln0Sn3+BsWoZdNxK7L2LO4',                                        'Content-Type': 'application/json'})                              'Content-Type': 'application/json'})但是,當我將它分為兩個類 - API 類和 Test 類時,找不到最后一個參數API類import requestsclass SignalsAdd:    def create_signal(self, signalType, currencyFrom, currencyTo, currencyPair,                            type, buyTip, stopTip, stopTipPips, takeProfits_value,                            isAchieved, closeOnReach, pips,                            status, orderType):        }
查看完整描述

2 回答

?
ABOUTYOU

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;頂層的普通函數工作得很好,并且通常是完成這項工作的最佳工具。


查看完整回答
反對 回復 2022-06-28
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

我認為明確傳遞 self 可能會導致問題。在調用函數時,嘗試使用變量名傳遞值,看看你得到了什么。在這里,您至少會知道哪個值傳遞給哪個變量以及缺少什么。

像這樣

        add_signal = SignalsAdd.create_signal(signal_type="crypto", currencyfrom="5c1e4633b140f7000f908897", ...)



查看完整回答
反對 回復 2022-06-28
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號