python - 缺少 1 个必需的位置参数 : 'key'

标签 python oop python-3.x

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。

最佳答案

你的意思可能是:

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()

关于python - 缺少 1 个必需的位置参数 : 'key' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694097/

相关文章:

python - 尽管出现 UnicodeDecodeError 错误,我如何继续处理文件?

javascript - 在大多数 OOP 语言中,实例方法中的 "i"是否首先引用本地,然后引用全局,但从不引用实例变量或类变量?

java - 从静态内部类中引用非静态变量

javascript - 什么时候应该在 JavaScript 中使用构造函数?

python - 将 string.decode ('utf8' ) 从 python2 转换为 python3

python - 我如何排除特定类型的 pymysql.err.InternalErrors,而不仅仅是所有类型?

Python lambda 函数未在 for 循环内正确调用

python - 格式化日期时间戳

python - 如何让程序只有在按下回车键时才继续运行?

python - Kivy Python TextInput 显示气泡