Why aren't colons optional?
Jason Orendorff
jason at jorendorff.com
Sun Jan 20 16:51:47 EST 2002
Roy Smith wrote:
> Can you give me a practical example of why somebody would want to
> define a __getitem__ method for a class?
To make objects that behave like lists or dictionaries, of course.
See e.g. the source code to the standard "os" module, where
os.environ is implemented as an instance of a class with a
__getitem__ method.
Or the standard "shelve" module, which provides a dictionary-like
object backed by a file.
Or a recent post on this newsgroup by Anton Vredegoor, where he
gave a class MenuChoice that had list-like behavior.
Etc.
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list