Re: Way for see if dict has a key
- From: Georg Brandl <g.brandl-nospam@xxxxxxx>
- Date: Fri, 30 Jun 2006 21:31:17 +0200
Bruno Desthuilliers wrote:
value = dict.get(key, None)
value = dict.get(key)
Yes - but :
1/ not everybody knows that dict.get() takes a second optional param.
Note that, while it happens that the default return value of dict.get()
is the same as in the above example, but it may not have been the case.
2/ Since dict.get() implicitely returns None while getattr() defaults to
raising an AttributeError unless you provide a default, I prefer to be
very explicit when using dict.get().
You're right, and I would write that with explicit None too.
Just wanted to continue the shortening ;)
Georg
.
- 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: looping
- Re: Way for see if dict has a key
- From: Bruno Desthuilliers
- Re: Way for see if dict has a key
- From: Georg Brandl
- Re: Way for see if dict has a key
- From: Bruno Desthuilliers
- Way for see if dict has a key
- Prev by Date: Re: efficiency question
- Next by Date: Re: Module executed twice when imported!
- 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
|