Python 对象没有属性“__getitem__”(类实例?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30774624/
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
Object has no attribute '__getitem__' (class instance?)
提问by Joseph Seung Jae Dollar
It might be a very simple question but I am very confused with where I am going right now. Here is a very basic class:
这可能是一个非常简单的问题,但我对我现在要去的地方感到非常困惑。这是一个非常基础的类:
class Book(object):
def __init__(self, title, price):
self.book = {'title':title, 'price':price}
And when I run this:
当我运行这个时:
book = Book('foo', 300)
book['price']
It spits out:
它吐出:
TypeError: 'Book' object has no attribute '__getitem__'
I know that it's not the conventional way of initializing an instance since I am using a dictionary. But I wonder why that code is spitting out a TypeError. How do I go about solving this issue? Thank you in advance.
我知道这不是初始化实例的传统方式,因为我使用的是字典。但我想知道为什么该代码会吐出 TypeError。我该如何解决这个问题?先感谢您。
ps. The book instance's type is class?
附:书实例的类型是类?
采纳答案by Henrik Andersson
It's because a class is not a dict object. Accessing properties on a instance of a class is done via the dot operator.
这是因为类不是 dict 对象。访问类实例的属性是通过点运算符完成的。
book.book['price']
>> 300
If you want to access keys within your dict directly on your class instance you would have to implement the __getitem__
method on your class.
如果您想直接在类实例上访问 dict 中的键,则必须在类上实现该__getitem__
方法。
def __getitem__(self, key):
return self.book[key]
book['price']
>> 300
回答by Aditya
Yes. bookis an object of class Bookbecause you initialized it that way.
是的。book是Book类的对象,因为您以这种方式对其进行了初始化。
book = Book('foo', 300)
book['price']
Try
尝试
print book.book['price']
print book.book['price']
So you want to access a dictionary called bookof an object referenced as bookand you want to extract the value of pricefrom the dictionary.
所以,你要访问一个名为字典的书作为参考对象的书,并要提取的价值价格从字典。
Usually [] operator looks for __getitem__()
method and passes the requested key as an argument. ?ukasz R. has shown how to do that. Dictionaries perform a key lookup while arrays find the slice or index.
通常 [] 运算符查找__getitem__()
方法并将请求的键作为参数传递。?ukasz R. 已经展示了如何做到这一点。字典执行键查找,而数组查找切片或索引。
It is explained in details here : https://docs.python.org/2/reference/datamodel.html
这里有详细解释:https: //docs.python.org/2/reference/datamodel.html
Now because you're creating a class here, why do you want to create a separate dictionary for native must-have attributes of the class. Create attributes like the following example:
现在,因为您在这里创建了一个类,为什么要为该类的本机必备属性创建一个单独的字典。创建类似以下示例的属性:
class Book(object):
def __init__(self, title, price):
self.title = 'foo'
self.price = 300
book = Book('foo', 300)
print book.title
回答by ?ukasz Rogalski
book.book['price']
would work.
For accessing proxy member you'll have to implement __getitem__
magic method.
book.book['price']
会工作。要访问代理成员,您必须实现__getitem__
魔术方法。
class Book(object):
def __init__(self, title, price):
self.book = {'title':title, 'price':price}
def __getitem__(self, item):
return self.book[item]
回答by Avión
class Book(object):
def __init__(self, title, price):
self.book = {'title':title, 'price':price}
book = Book('foo', 300)
print book.book['price']