元组对象没有属性追加(python)

ahr*_*hri 1 python

我有两个文件:

animal.py有以下内容:

class Rabbit:

def __init__(self):
    self.list = [],    

def add(self, val):
    self.list.append(val)
Run Code Online (Sandbox Code Playgroud)

ttt.py有以下内容:

from animal import Rabbit

r = Rabbit()
r.add(1)
Run Code Online (Sandbox Code Playgroud)

我继续得到一个attributeError说tuple对象没有属性'append'

但是,我查看了list = [].这不是一个元组我应该如何解决它?

Mar*_*ers 5

删除逗号:

self.list = [],    
#  this comma ^
Run Code Online (Sandbox Code Playgroud)

在Python中,逗号是使元组成为元组的逗号 ; 仅需要括号来消除可能也使用逗号的其他语法的元组歧义.