如果一个类实例是另一个类实例的属性,如何更正错误1位置参数缺失?

2024-06-28 20:07:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python3.x中,我如何更正这个TypeError,指出对一个类实例的属性的函数调用缺少1个必需的位置参数,而该属性本身就是另一个类的实例

代码显示在下面,错误消息显示在下面。错误消息指示的代码行是最后一行代码


class Restaurant:
    """A simple representation of a restaurant."""
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(f"Restaurant name: {self.restaurant_name.title()}")
        print(f"Cuisine: {self.cuisine_type}")

toy_cafe = Restaurant("toy cafe", "toy food")
pet_cafe = Restaurant("pet cafe", "pet food")

toy_cafe.describe_restaurant()
print("")

pet_cafe.describe_restaurant()
print("")


class Flavours:
    """Create a class to store a list of flavours."""
    def __init__(self, restaurant_name):
        self.flavours = ['strawberry', 'chocolate', 'vanilla', 'mint', 'cookies and cream', 'peach', 'mango', 'coconut', 'coffee', 'caramel', 'choc mint', 'lemon', 'choc chip', 'passionfruit']
    
    def show_flavours(self, restaurant_name):
        """prints a list of icecream flavours sold by an icecream stand."""
        print(f"\n{self.restaurant_name.title()} serves these icecream flavours:\n")
        for flavour in flavours:
            print(' -', flavour)

class Icecream_stand(Restaurant):
    """Create a class for icecream stands."""
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__(restaurant_name, cuisine_type)
        self.flavours_sold = Flavours(self.restaurant_name) 

toy_icecream = Icecream_stand("toy icecream", "icecream")

toy_icecream.flavours_sold.show_flavours()

toy_icecream.flavours_sold.show_flavours()
TypeError: show_flavours() missing 1 required positional argument: 'restaurant_name'

我试着把我能想到的所有东西都放在函数调用的括号内 (例如:self.self.restaurant\u name restaurant\u name toy\u icecream.restaurant\u name) 但其中前三个给定名称错误:未定义名称“”, 最后一个给AttributeError:“Flavours”对象没有属性“restaurant\u name”

如何修复此代码

为什么我试着放在括号内的每个参数都会返回这些错误

谢谢大家!


Tags: 代码nameselfcafedeftype错误restaurant
2条回答
class Restaurant:
    """A simple representation of a restaurant."""
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print(f"Restaurant name: {self.restaurant_name.title()}")
        print(f"Cuisine: {self.cuisine_type}")

toy_cafe = Restaurant("toy cafe", "toy food")
pet_cafe = Restaurant("pet cafe", "pet food")

toy_cafe.describe_restaurant()
print("")

pet_cafe.describe_restaurant()
print("")


class Flavours:
    """Create a class to store a list of flavours."""
    def __init__(self, restaurant_name):
        self.flavours = ['strawberry', 'chocolate', 'vanilla', 'mint', 'cookies and cream', 'peach', 'mango', 'coconut', 'coffee', 'caramel', 'choc mint', 'lemon', 'choc chip', 'passionfruit']
        self.restaurant_name = restaurant_name
    def show_flavours(self):
        """prints a list of icecream flavours sold by an icecream stand."""
        print(f"\n{self.restaurant_name.title()} serves these icecream flavours:\n")
        for flavour in self.flavours:
            print(' -', flavour)

class Icecream_stand(Restaurant):
    """Create a class for icecream stands."""
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__(restaurant_name, cuisine_type)
        self.flavours_sold = Flavours(self.restaurant_name) 

toy_icecream = Icecream_stand("toy icecream", "icecream")

toy_icecream.flavours_sold.show_flavours()

所有这些问题都存在于Flavours类中。您需要进行以下修改:

class Flavours:
    """Create a class to store a list of flavours."""
    def __init__(self, restaurant_name):
        self.restaurant_name = restaurant_name   #< - add this
        self.flavours = ['strawberry', 'chocolate', 'vanilla', 'mint', 'cookies and cream', 'peach', 'mango', 'coconut', 'coffee', 'caramel', 'choc mint', 'lemon', 'choc chip', 'passionfruit']
    
    def show_flavours(self):   #< - remove `restaurant_name` from here
        """prints a list of icecream flavours sold by an icecream stand."""
        print(f"\n{self.restaurant_name.title()} serves these icecream flavours:\n")
        for flavour in self.flavours:
            print(' -', flavour)

相关问题 更多 >