3 回答
TA贡献1798条经验 获得超3个赞
我认为这显示了如何做你想做的事。我还修改了您的代码以遵循PEP 8 - Python 代码风格指南并更正了一些拼写错误的单词。
class Coffee: # Class names should Capitalized.
coffeelist = [] # Class attribute to track instance names.
def __init__(self,name,origin,price):
self.name = name
self.origin = origin
self.price = price
self.coffeelist.append(self.name)
def print_coffeelist(self):
print(self.coffeelist)
c1 = Coffee("blackcoffee", "tanz", 55)
c1.print_coffeelist() # -> ['blackcoffee']
c2 = Coffee("fineroasted", "ken", 60)
c1.print_coffeelist() # -> ['blackcoffee', 'fineroasted']
# Can also access attribute directly through the class:
print(Coffee.coffeelist) # -> ['blackcoffee', 'fineroasted']
TA贡献1840条经验 获得超5个赞
是的,谢谢,这正是我想要的!我不确定.. 我以为你可以在 return 语句中同时做两件事,都返回追加。我想很多时候python非常灵活,有时则不然。谢谢
添加回答
举报