Re: Way for see if dict has a key
- From: "Paul McGuire" <ptmcg@xxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Jun 2006 14:07:28 GMT
"Michele Petrazzo" <michele.petrazzo@xxxxxxxxxxxxxx> wrote in message
news:zZ7pg.21792$zy5.445667@xxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: Way for see if dict has a key
- From: Fredrik Lundh
- Re: Way for see if dict has a key
- References:
- Way for see if dict has a key
- From: Michele Petrazzo
- Re: Way for see if dict has a key
- From: Bruno Desthuilliers
- Re: Way for see if dict has a key
- From: Michele Petrazzo
- Re: Way for see if dict has a key
- From: André
- Re: Way for see if dict has a key
- From: Michele Petrazzo
- Way for see if dict has a key
- Prev by Date: print shell output in a file
- Next by Date: RE: string replace
- Previous by thread: Re: Way for see if dict has a key
- Next by thread: Re: Way for see if dict has a key
- Index(es):
Relevant Pages
|