Python将实例用作属性 里面的方法修改不了属性

class Restaurant():
"""餐馆"""

def __init__(self,restaurant_name,*cuisine_type):
    """初始化餐换"""

    self.restaurant_name=restaurant_name
    self.cuisine_type=cuisine_type

def describe_restaurant(self):
    """打印餐馆信息"""
    print("The restaurant's name is "+ self.restaurant_name)
    print("The restaurant have the following cuisine_type "+ str(self.cuisine_type))

def open_restaurant(self):
    """营业"""
    print("The "+self.restaurant_name+" is Open\n")

restaurant=Restaurant('餐馆','食物')
restaurant.describe_restaurant()
restaurant.open_restaurant()

class Show():
"""列出列表"""

def __init__(self,flavors):
    self.flavors=flavors

def show_flavors(self):
    """列出口味"""
    print("There are the following flavors:")
    for flavor in self.flavors:
        print(flavor)

class IceCreamStand(Restaurant):
"""餐馆里面卖冰淇淋"""

"""初始化餐馆"""
def __init__(self,restaurant_name,*cuisine_type):
    """初始化父类的属性"""
    super().__init__(restaurant_name,*cuisine_type)

    self.flavors=['草莓']
    self.show=Show(self.flavors)

icecream=IceCreamStand('冰淇淋店','冰淇淋')

icecream.flavors=['草莓','西瓜','香草','蓝莓']

print(icecream.flavors)

icecream.describe_restaurant()

icecream.open_restaurant()

icecream.show.show_flavors()

为什么修改flavors属性后直接打印显示修改成功了,但用将实例用作属性使用里面的方法列出来得到还是没有修改的值

The restaurant's name is 餐馆

The restaurant have the following cuisine_type ('食物',)

The 餐馆 is Open

['草莓', '西瓜', '香草', '蓝莓']

The restaurant's name is 冰淇淋店

The restaurant have the following cuisine_type ('冰淇淋',)

The 冰淇淋店 is Open

There are the following flavors:

草莓


你的代码并没有改变Show里面的属性,而是改了IceCream里面的。Debug一下可以看到。
图片说明

改了一下你的代码

class Restaurant():
    """餐馆"""

    def __init__(self,restaurant_name,*cuisine_type):
        """初始化餐换"""

        self.restaurant_name=restaurant_name
        self.cuisine_type=cuisine_type

    def describe_restaurant(self):
        """打印餐馆信息"""
        print("The restaurant's name is "+ self.restaurant_name)
        print("The restaurant have the following cuisine_type " + str(self.cuisine_type))

    def open_restaurant(self):
        """营业"""
        print("The "+self.restaurant_name+" is Open\n")


restaurant=Restaurant('餐馆', '食物')
restaurant.describe_restaurant()
restaurant.open_restaurant()


class Show():
    """列出列表"""

    def __init__(self, flavors):
        self.flavors = flavors

    def show_flavors(self):
        """列出口味"""
        print("There are the following flavors:")
        for flavor in self.flavors:
            print(flavor)


class IceCreamStand(Restaurant):
    """餐馆里面卖冰淇淋"""

    """初始化餐馆"""
    def __init__(self, restaurant_name, *cuisine_type):
        """初始化父类的属性"""
        super().__init__(restaurant_name,*cuisine_type)

        self.flavors = ['草莓']

    def show_fla(self):
        show = Show(self.flavors)
        show.show_flavors()


icecream = IceCreamStand('冰淇淋店', '冰淇淋')

icecream.flavors = ['草莓','西瓜','香草','蓝莓']

print(icecream.flavors)

icecream.describe_restaurant()

icecream.open_restaurant()

icecream.show_fla()