On 10/04/20 11:43 pm, Soni L. wrote:
it's actually fairly common to deal with KeyError instead of using
dict.get or w/e.
When doing that, though, it's good practice to make sure the
try/except encloses the minimum amount of code needed.
E.g. instead of
try:
value = somedict[get_my_key()]
except KeyError:
value = something_else()
it's better to write
k = get_my_key()
try:
value = somedict[k]
except KeyError:
value = something_else()
Then there's no chance of accidentally swallowing a KeyError
produced by get_my_key().
--
Greg
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/DMRVURQH6RAHUVFRLCO2NLSPSLAZRZR3/
Code of Conduct: http://python.org/psf/codeofconduct/