I have an object with a dictionary that I want to access via __getitem__
as well as iterate over (values only, keys don't matter) but am not sure how to do it.
For example:
Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10)
>>> class Library(object):
... def __init__(self):
... self.books = { 'title' : object, 'title2' : object, 'title3' : object, }
... def __getitem__(self, i):
... return self.books[i]
...
>>> library = Library()
>>> library['title']
<type 'object'>
>>> for book in library:
... print book
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __getitem__
KeyError: 0
>>>
How do I tell it to simply return the object
for each item in the dictionary (the key doesn't matter) ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…