Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError:“tuple”对象没有属性“weight”_Python_Python 3.x_Attributeerror - Fatal编程技术网

Python AttributeError:“tuple”对象没有属性“weight”

Python AttributeError:“tuple”对象没有属性“weight”,python,python-3.x,attributeerror,Python,Python 3.x,Attributeerror,我不知道我做错了什么。我在这里写的每一件事都来自于这本书。ap1不是苹果的一个实例,它只是一个简单的元组。您需要显式调用Apple的Constructor: class Apple: def __init__( self,w,c): self.weight=w self.color=c print("utworzono!") ap1=(280,"red") print(ap1) print(ap1.weight) 实际上,您使用的是

我不知道我做错了什么。我在这里写的每一件事都来自于这本书。

ap1不是苹果的一个实例,它只是一个简单的元组。您需要显式调用Apple的Constructor:

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,红色,而不是api1=Apple280,红色。你可以阅读更多关于。
ap1 = Apple(280, "red")