"Michele Petrazzo" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > when we expect that the key will most often be in my_dict so that > > exception will be raised rarely > > I didn't thought this because if I think that a key aren't in a dict, I > use dict.get(key, default) >
Another factor to consider is if "default" is not something simple like 0 or None, but is an object constructed and initialized with some expensive initialization code (like access to a database). In this case: dict.get(key, ExpensiveObjectToCreate()) always creates the default value, and if the key does not exist, then promptly drops it on the floor. In such a case you'd be better off with one of the "check for existence" patterns, which only creates the default value if you *know* you're going to need it. -- Paul -- http://mail.python.org/mailman/listinfo/python-list