【问题标题】:AttributeError: 'Test' object has no attribute 'a'AttributeError:“测试”对象没有属性“a”
【发布时间】:2020-08-29 01:33:42
【问题描述】:

我收到 AttributeError: 'Test' object has no attribute 'a'。我不明白代码有什么问题。请指教。

class Test():
    def __int__(self):
        self.a = 1

t = Test()
print(t.a)

【问题讨论】:

标签: python python-3.x


【解决方案1】:

应该是 init(initialise 的缩写),而不是 int(整数):

class Test():
    # here was the fault
    def __init__(self):
        self.a = 1

t = Test()
print(t.a)
# Outputs 1

你定义了错误的方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 2017-08-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多