UserDict's has_key() and get() should use __getitem__()

Andrew Dalke dalke at acm.org
Thu Apr 6 07:47:13 EDT 2000


> * Do we know how much less efficient?

Take a look at Barry Warsaw's paper at

 http://www.python.org/~bwarsaw/ecshort.pdf .

The answer is a question - will the lookup mostly fail or
mostly succeed?

] This indicates that the has_key() idiom is usually the best
] one to choose, both because it is usually faster than the
] exception idiom, and because its costs are less variable.
] However, it does indicate that a common pattern that, for
] example, uses dictionaries to count events may be better off
] using the exception idiom, since it is likely that the key
] will be missing from the dictionary only once.

For what you're proposing, there's another method lookup
(since has_key() turns around to call self.__getitem__(key) )
which makes your proposal even slower than those numbers.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list