关于PYTHON的类
class Restaurant():"""class of restaurant"""
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("Restaurant's name is " + self.restaurant_name)
print("Restaurant's type is " + self.cuisine_type)
def open_restaurant(self):
print('This restaurant is openning.')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors = ['cheese']
def show_flavors(self):
print(self.flavors)
def flavors(self,flavors_list):
self.flavors = self.flavors.append(flavors_list)
M = IceCreamStand('Mc','western')
M.flavors()
M.show_flavors()
我希望在冰淇淋类在继承父类之后添加一个属性,属性值是一个列表
第29行M.flavors()和最后一个flavors函数应该怎么修改? ???这不是没问题吗? 永恒的蓝色梦想 发表于 2020-9-16 20:13
???这不是没问题吗?
Traceback (most recent call last):
File "9-6.py", line 29, in <module>
M.flavors()
TypeError: 'list' object is not callable
然而报错了 张图南 发表于 2020-9-16 20:22
Traceback (most recent call last):
File "9-6.py", line 29, in
M.flavors()
class Restaurant():
"""class of restaurant"""
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("Restaurant's name is " + self.restaurant_name)
print("Restaurant's type is " + self.cuisine_type)
def open_restaurant(self):
print('This restaurant is openning.')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors = ['cheese']
def show_flavors(self):
print(self.flavors)
def flavors(self,flavors_list):
self.flavors.append(flavors_list)
M = IceCreamStand('Mc','western')
M.flavors()
M.show_flavors() 永恒的蓝色梦想 发表于 2020-9-16 20:31
还是不行,M.flavors()的括号里放整型、字符串和列表都会报错 你改成self.flavors = flavors.append(flavors_list)试试 bonst 发表于 2020-9-16 21:26
你改成self.flavors = flavors.append(flavors_list)试试
还是报错 张图南 发表于 2020-9-16 21:00
还是不行,M.flavors()的括号里放整型、字符串和列表都会报错
class Restaurant():
"""class of restaurant"""
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("Restaurant's name is " + self.restaurant_name)
print("Restaurant's type is " + self.cuisine_type)
def open_restaurant(self):
print('This restaurant is openning.')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors = ['cheese']
def show_flavors(self):
print(self.flavors)
def flavors(self,flavors_list):
self.flavors.extend(flavors_list)
M = IceCreamStand('Mc','western')
M.flavors()
M.show_flavors() 永恒的蓝色梦想 发表于 2020-9-17 21:21
还是报错了,M.flavors()里放啥都报错
页:
[1]