【问题标题】:missing 1 required positional argument: 'key'缺少 1 个必需的位置参数:'key'
【发布时间】:2015-08-22 00:45:03
【问题描述】:
class Keys():
    def __init__(self):
        self.key_list = {1:"one", 2:"two", 3:"three"}
    def get_name(self, key):
       self.ddd = key

key1 = Keys
key1.get_name(1)

为什么,在启动这段代码后,我会得到这个错误:

Traceback (most recent call last):
  File "class.py", line 8, in <module>
    key1.get_name(1)
TypeError: get_name() missing 1 required positional argument: 'key'

我正在使用 Python 3。

【问题讨论】:

  • 创建类实例的方式不对。应该是key1 = Keys()
  • 是的!非常感谢!

标签: python oop python-3.x


【解决方案1】:

你的意思可能是:

class Keys():
    def __init__(self):
        self.key_list = {1:"one", 2:"two", 3:"three"}
    def get_name(self, key):
       self.ddd = key

key1 = Keys()
key1.get_name(1)

注意括号的使用:key1 = Keys()

【讨论】:

    【解决方案2】:

    您缺少括号:

    class Keys():
        def __init__(self):
            self.key_list = {1:"one", 2:"two", 3:"three"}
        def get_name(self, key):
           self.ddd = key
    
    key1 = Keys() # <- missing parens
    key1.get_name(1)
    

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2017-03-27
      • 2019-11-12
      • 2019-12-23
      相关资源
      最近更新 更多