为了账号安全,请及时绑定邮箱和手机立即绑定

发生“缺少一个位置参数”错误,但所有参数都存在

发生“缺少一个位置参数”错误,但所有参数都存在

呼如林 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": "admin02@thinkmelius.com", "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 关注
  • 179 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号