简体   繁体   English

缺少必需的位置参数:Python: self

[英]missing required positional arguments: Python: self

Getting an error :得到一个错误:

TypeError: _get_request() missing 6 required positional arguments: 'id_list', 'id_item', 'to_be_sent', 'open', 'received', and 'sent'

When I am trying to call the function:当我尝试调用该函数时:

def _get_request(self, id: str,
                           id_list: List[str],
                           id_item: List[int],
                           to_be_sent: List[int],
                           open: List[int],
                           received: List[int],
                           sent: List[int]):



    return {
            'id': user_id,
            'num_results': 3,
            'id_list': uniqueVisitId,
            'id_item': id_item,
            'to_be_sent': need_to_be_sent,
            'open': open,
            'received': received,
            'sent': sent
    }

By:经过:

self._get_request(
          {
           'id': 'x',
            'id_list': ['a', 'x'],
            'id_item': [1, 2],
            'to_be_sent': [1, 0],
            'open': [0, 0],
            'received': [0, 0],
            'sent': [0, 0]
          }
     )

Any guess why my function cant recognize the positionial arguments which I am passing over?任何猜测为什么我的函数无法识别我传递的位置参数?

Because you are passing a dictionary and not arguments...因为你传递的是字典而不是参数......

You should pass the arguments in order without Dict syntax:您应该在没有 Dict 语法的情况下按顺序传递参数:

self._get_request('x', ['a', 'x'], [1, 2],  [1, 0],   [0, 0],[0, 0], [0, 0])

Use ** before the dict items to parse them as key=value in functions在 dict 项之前使用**将它们解析为函数中的 key=value

def foo(*, a, b):
    return a, b


dict = {'a': 1, 'b': 2}
print(foo(dict)) # -> missing
print(foo(**dict)) # -> returns (1, 2)

You are passing it only one argument — a dictionary.你只传递了一个参数——一本字典。 If you are just writing specific calls to get_request(), and/or the dictionary will always have that set of specified keys, do what adir said.如果您只是编写对 get_request() 的特定调用,和/或字典将始终具有该组指定的键,请执行 adir 所说的操作。 If the dictionary you are passing it comes from somewhere else and may have varying keywords.如果您传递的字典来自其他地方,并且可能有不同的关键字。 then you can use dictionary unpacking like this:那么你可以像这样使用字典解包:

# Assigning it just for the example, might be the result from a function or whatever you're doing
request_info_dict = {
                     'id': 'x',
                     'id_list': ['a', 'x'],
                     'id_item': [1, 2],
                     'to_be_sent': [1, 0],
                     'open': [0, 0],
                     'received': [0, 0],
                     'sent': [0, 0]
                   }
self._get_request(**request_info_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM