初学者 Python:AttributeError:'list' 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29335423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:26:02  来源:igfitidea点击:

Beginner Python: AttributeError: 'list' object has no attribute

pythonlistclassdictionaryattributeerror

提问by Charles Watson

The error says:

错误说:

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

I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles:

我正在尝试使用以下类来进行简单的利润计算来处理自行车字典:

class Bike(object):
    def __init__(self, name, weight, cost):
        self.name = name
        self.weight = weight
        self.cost = cost

bikes = {
    # Bike designed for children"
    "Trike": ["Trike", 20, 100],
    # Bike designed for everyone"
    "Kruzer": ["Kruzer", 50, 165]
    }

When I try to calculate profit with my for statement, I get the attribute error.

当我尝试使用 for 语句计算利润时,出现属性错误。

# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
    profit = bike.cost * margin

First, I don't know why it is referring to a list, and everything seems to be defined, no?

首先,我不知道为什么它指的是一个列表,而且一切似乎都已定义,不是吗?

采纳答案by jedwards

Consider:

考虑:

class Bike(object):
    def __init__(self, name, weight, cost):
        self.name = name
        self.weight = weight
        self.cost = cost

bikes = {
    # Bike designed for children"
    "Trike": Bike("Trike", 20, 100),      # <--
    # Bike designed for everyone"
    "Kruzer": Bike("Kruzer", 50, 165),    # <--
    }

# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
    profit = bike.cost * margin
    print(profit)

Output:

输出:

33.0
20.0

The difference is that in your bikesdictionary, you're initializing the values as lists [...]. Instead, it looks like the rest of your code wants Bikeinstances. So create Bikeinstances: Bike(...).

不同之处在于,在您的bikes字典中,您将值初始化为列表[...]。相反,看起来您的其余代码需要Bike实例。所以创建Bike实例:Bike(...).

As for your error

至于你的错误

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

this will occur when you try to call .coston a listobject. Pretty straightforward, but we can figure out what happened by looking at where you call .cost-- in this line:

这样当您尝试调用将出现.cost一个上list对象。非常简单,但我们可以通过查看您调用的位置来弄清楚发生了什么.cost- 在这一行中:

profit = bike.cost * margin

This indicates that at least one bike(that is, a member of bikes.values()is a list). If you look at where you defined bikesyou can see that the values were, in fact, lists. So this error makes sense.

这表明至少有一个bike(即,一个成员bikes.values()是一个列表)。如果您查看定义的位置,bikes您会发现这些值实际上是列表。所以这个错误是有道理的。

But since your classhas a cost attribute, it looked like you were trying to use Bikeinstances as values, so I made that little change:

但是因为你的类有一个 cost 属性,看起来你试图使用Bike实例作为值,所以我做了一点改变:

[...] -> Bike(...)

and you're all set.

你已经准备好了。

回答by Jake Kreider

You need to pass the values of the dict into the Bikeconstructor before using like that. Or, see the namedtuple-- seems more in line with what you're trying to do.

Bike在使用之前,您需要将 dict 的值传递给构造函数。或者,查看namedtuple-- 似乎更符合您要执行的操作。

回答by Ygedey

They are lists because you type them as lists in the dictionary:

它们是列表,因为您在字典中将它们作为列表键入:

bikes = {
    # Bike designed for children"
    "Trike": ["Trike", 20, 100],
    # Bike designed for everyone"
    "Kruzer": ["Kruzer", 50, 165]
    }

You should use the bike-class instead:

您应该改用自行车类:

bikes = {
    # Bike designed for children"
    "Trike": Bike("Trike", 20, 100),
    # Bike designed for everyone"
    "Kruzer": Bike("Kruzer", 50, 165)
    }

This will allow you to get the cost of the bikes with bike.cost as you were trying to.

这将使您能够像您尝试使用的那样通过 bibk.cost 获得自行车的成本。

for bike in bikes.values():
    profit = bike.cost * margin
    print(bike.name + " : " + str(profit))

This will now print:

现在将打印:

Kruzer : 33.0
Trike : 20.0