【问题标题】:AttributeError: 'tuple' object has no attribute 'weight'AttributeError:“元组”对象没有属性“权重”
【发布时间】:2019-06-20 14:10:47
【问题描述】:
class Apple:
    def __init__( self,w,c):
        self.weight=w
        self.color=c
        print("utworzono!")



ap1=(280,"red")
print(ap1)
print(ap1.weight)

我不知道我做错了什么。我在这里写的所有东西都是书本。

【问题讨论】:

  • 你实际上做了api1 = (280, "red")而不是api1 = Apple(280, "red")。你可以阅读更多关于tuples的信息。

标签: python python-3.x attributeerror


【解决方案1】:

ap1 不是Apple 的实例,它只是一个普通的旧元组。你需要显式调用Apple的构造函数:

ap1 = Apple(280, "red")

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2021-07-30
    • 2013-07-29
    • 2020-08-29
    • 2015-04-22
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多