“列表”对象没有属性,为什么?

时间:2019-08-05 13:09:41

标签: python python-3.x list

我正在尝试通过类参数传递项目列表,并调用方法display_flavors以打印列表。但是我收到了list object has no attribute error

我已经重写了有效的代码,但是我想重写代码以在列表中传递,而不是调用方法并通过list参数传递。可能吗?

# Python script Idea to be implemented which has an error
class IceCreamStand(Restaurant):  # Inherits the Parent Class "Restaurant"
    """Attempt to represent an Ice Cream Stand"""
    def __init__(self, restaurant_name, cuisine_name, flavors):
        """Initialize the attributes of an Ice Cream Stand"""
        super().__init__(restaurant_name, cuisine_name) 
        self.flavors = flavors

    def display_flavors(self):
        """Print all the flavors the Ice Cream Stand offers"""
        print("These are flavors offered in this Ice Cream Stand:")
        for flavor in self.flavors:
            print(flavor.title())

list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream", list_of_flavors)
restaurant1.flavors.display_flavors() 

================================================ ======================== 重写的代码有效

class IceCreamStand(Restaurant):  # Inherits Parent Class "Restaurant"
     """Attempt to represent an Ice Cream Stand"""
    def __init__(self, restaurant_name, cuisine_name):
        """Initialize the attributes of an Ice Cream Stand"""
        super().__init__(restaurant_name, cuisine_name)

    def display_flavors(self, flavors):
        """Print all the flavors the Ice Cream Stand offers"""
        print("These are flavors offered in this Ice Cream Stand:")
        for flavor in flavors:
            print(flavor.title())

list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream")
restaurant1.display_flavors(list_of_flavors)

================================================ =======================

AttributeError: 'list' object has no attribute 'display_flavors'

2 个答案:

答案 0 :(得分:1)

使用点运算符调用函数时,通常会以第一个参数为点运算符之前的形式调用该函数。这就是为什么在类public class Actions : Entity<long>, IMustHaveTenant { [Column("ActionId")] [Key] public override long Id { get; set; } [Required] public string DeviceId { get; set; } [Required] public int TenantId { get; set; } public virtual ICollection<ActionsDetails> ActionsDetails { get; set; } public Actions() { ActionsDetails = new List<ActionsDetails>(); } } public class ActionsDetails : Entity<long> { [ForeignKey("ActionId")] public virtual Actions Actions { get; set; } [Column("ActionsDetailsId")] [Key] public override long Id { get; set; } [Required] public long ActionId { get; set; } [Required] public string ActionName { get; set; } [Required] public string Description { get; set; } } 之类的方法中编写ActionsDetails的原因。

这样做的时候

self

然后Python尝试在对象display_flavors上调用方法restaurant1.flavors.display_flavors() ,而没有其他参数。但是,display_flavors()是一个列表,因此没有名为restaurant1.flavors的方法。因此,您的restaurant1.flavors

同时,当您这样做

display_flowers()

您正在AttributeError上调用方法restaurant1.display_flavors(list_of_flavors) -这是一个display_flavors(),并且确实有一个称为该方法的方法。所述方法有两个参数:restaurant1(作为IceCreamStand)和restaurant1

因此,在第一个示例中使用self代替list_of_flavors应该可以。

答案 1 :(得分:0)

现在可以使用了!!谢谢大家的澄清,这个错误永远困扰着

# Corrected Script
class IceCreamStand(Restaurant):  # Inherits the attributes from Parent Class "Restaurant"
    """Attempt to represent an Ice Cream Stand"""

    def __init__(self, restaurant_name, cuisine_name, flavors):
        """Initialize the attributes of an Ice Cream Stand"""
        super().__init__(restaurant_name, cuisine_name)
        self.flavors = flavors

    def display_flavors(self):
        """Print all the flavors the Ice Cream Stand offers"""
        print("These are flavors offered in this Ice Cream Stand:")
        for flavor in self.flavors:
            print(flavor.title())


list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream", list_of_flavors)
restaurant1.display_flavors()  # <<< Here is the correction 

输出:

    These are flavors offered in this Ice Cream Stand:
    Chocolate
    Strawberry
    Banana