在一个属性中存储字符串列表
Store a list of strings in one attribute
我想我可能没有把我的问题解释清楚。我为此道歉。我会再试一次。
我有一个 parent class 具有某些属性:
class Restaurant():
'This is the restaurant class'
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
然后我有一个 child class 继承了 parent 的所有属性并添加了一个新属性:
class IceCreamStand():
def __init__(self, *flavor):
'Attributes of parent class initialised'
self.flavor = flavor
现在我尝试打印存储在属性 flavor 中的口味列表:
def desc_flavor(self):
print('This Ice_Cream shop has ' + self.flavor + ' flavors')
flavor1 = IceCreamStand('Mango', 'Raspberry', 'Coffee', 'Vanilla')
如果我使用 concat,我会收到消息说名称未定义。
很抱歉第一次没有正确解释问题,感谢大家的帮助。
尝试使用以下代码:
def __init__(self, *attribute1):
self.atributte1 = attribute1
使用Arbitrary argument list. See this答案。
示例:
li = []
def example(*arg):
li = list(arg)
print(li)
example('string1', 'string2', 'string3')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
def describe_flavors(self,*flavors):
print(f'{self.restaurant_name} has the following flavors:')
for self.flavor in flavors:
print(f'-{self.flavor}')
restaurant =IceCreamStand('DQ','ice cream')
restaurant.describe_restaurant()
restaurant.describe_flavors('Chocolate','Mango', 'Raspberry', 'Coffee', 'Vanilla')
我想我可能没有把我的问题解释清楚。我为此道歉。我会再试一次。
我有一个 parent class 具有某些属性:
class Restaurant():
'This is the restaurant class'
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
然后我有一个 child class 继承了 parent 的所有属性并添加了一个新属性:
class IceCreamStand():
def __init__(self, *flavor):
'Attributes of parent class initialised'
self.flavor = flavor
现在我尝试打印存储在属性 flavor 中的口味列表:
def desc_flavor(self):
print('This Ice_Cream shop has ' + self.flavor + ' flavors')
flavor1 = IceCreamStand('Mango', 'Raspberry', 'Coffee', 'Vanilla')
如果我使用 concat,我会收到消息说名称未定义。
很抱歉第一次没有正确解释问题,感谢大家的帮助。
尝试使用以下代码:
def __init__(self, *attribute1):
self.atributte1 = attribute1
使用Arbitrary argument list. See this答案。
示例:
li = []
def example(*arg):
li = list(arg)
print(li)
example('string1', 'string2', 'string3')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
def describe_flavors(self,*flavors):
print(f'{self.restaurant_name} has the following flavors:')
for self.flavor in flavors:
print(f'-{self.flavor}')
restaurant =IceCreamStand('DQ','ice cream')
restaurant.describe_restaurant()
restaurant.describe_flavors('Chocolate','Mango', 'Raspberry', 'Coffee', 'Vanilla')