我不太明白的基本 python 语法

basic python syntax that i don't quite get

我一直收到这个错误,但我不确定为什么。

Traceback (most recent call last):
  File "/home/cambria/Main.py", line 1, in <module>
    from RiotAPI import RiotAPI
  File "/home/cambria/RiotAPI.py", line 6
    def __init__(self, api_key, region=Consts.REGIONS['north_america'])
                                                                      ^
SyntaxError: invalid syntax

我已经很久没有使用 Python 了,我使用它只是因为它有助于我努力做好的事情,但据我所知,我使用过各种其他语言想在此声明 def __init__(self, api_key, region=Consts.REGIONS['north_america']) 中关闭这些 ()'s 但是我一直收到 SyntaxError: invalid syntax?

如果有帮助,该定义的其余部分如下。

class RiotAPI(object):
    def __init__(self, api_key, region=Consts.REGIONS['north_america'])
        self.api_key = api_key
        self.region = region

编辑 1:如果我像这样在 def __init__(self, api_key, region=Consts.REGIONS['north_america']): 末尾添加 :,为什么?这样做之后,我得到了一个新的语法错误,我将在一些明智的做法后解决

编辑 2:修复第一个错误后的新语法错误是,

Traceback (most recent call last):
  File "/home/cambria/Main.py", line 1, in <module>
    from RiotAPI import RiotAPI
  File "/home/cambria/RiotAPI.py", line 11
    args = ('api_key': self.api_key)
                     ^
SyntaxError: invalid syntax

def _request(self, api_url, params=()):
    args = ('api_key': self.api_key)
    for key, value in params.items():
        if key not in args:
            args[key] = value

编辑 3:这应该是最后一个了.. 没有更多的语法,只是一个

Traceback (most recent call last):
  File "/home/cambria/Main.py", line 10, in <module>
    main()
  File "/home/cambria/Main.py", line 5, in main
    respons3 = api.get_summoner_by_name('hi im gosan')
  File "/home/cambria/RiotAPI.py", line 31, in get_summoner_by_name
    return self._request(api_url)
  File "/home/cambria/RiotAPI.py", line 12, in _request
    for key, value in params.items():
AttributeError: 'tuple' object has no attribute 'items'

def _request(self, api_url, params=()):
        args = {'api_key': self.api_key}
        for key, value in params.items():
            if key not in args:
                args[key] = value
        response = requests.get(
            Consts.URL['base'].format(
                proxy=self.region,
                region=self.region,
                url=api_url
                ),
            params=args
            )
        print response.url
        return response.json()

这是我收到的唯一一个我真的不太了解的错误。这是我的参数没有 .items 的结果吗?或者我把它初始化为一个空字典?

问题是您在行尾缺少一个 :。

def __init__(self, api_key, region=Consts.REGIONS['north_america']):
    self.api_key = api_key
    self.region = region

您忘记了 ::

class RiotAPI(object):
    def __init__(self, api_key, region=Consts.REGIONS['north_america']): # <HERE
        self.api_key = api_key
        self.region = region