Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
ps. The book instance's type is class?
It's because a class is not a dict object.
Accessing properties on a instance of a class is done via the dot operator.
book.book['price']
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.
def __getitem__(self, key):
return self.book[key]
book['price']
Yes. book is an object of class Book because you initialized it that way.
book = Book('foo', 300)
book['price']
print book.book['price']
So you want to access a dictionary called book of an object referenced as book and you want to extract the value of price from 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.
It is explained in details here : 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
book.book['price'] would work.
For accessing proxy member you'll have to implement __getitem__ magic method.
class Book(object):
def __init__(self, title, price):
self.book = {'title':title, 'price':price}
def __getitem__(self, item):
return self.book[item]
class Book(object):
def __init__(self, title, price):
self.book = {'title':title, 'price':price}
book = Book('foo', 300)
print book.book['price']
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.