Re: Way for see if dict has a key



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
.



Relevant Pages

  • Re: Metaprogramming Example
    ... On Apr 18, 4:48 am, Bruno Desthuilliers <bruno. ... to do with making namespaces explicit in some places and not others). ... are explicit or implicit. ... lookup) and "self". ...
    (comp.lang.python)
  • Re: Pythonic emptiness checking
    ... On Apr 29, 6:46 am, Bruno Desthuilliers <bruno. ... Because it is explicit that in Python, ... called the Cold Hard Rules of Python"; in this case the language goes ...
    (comp.lang.python)
  • Re: Does python have the static function member like C++
    ... On Apr 11, 9:09 am, Bruno Desthuilliers <bruno. ... def counter_increase: ... explicit type.counter_increase ...
    (comp.lang.python)
  • Re: about __str__
    ... Bruno Desthuilliers wrote: ... More explicit IMHO. ... especially if the string would contain several insertions. ...
    (comp.lang.python)