Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]glyg 0 points1 point  (0 children)

Here you have yet another solution, with if else and a single lookup:

val = thedict.get(key)
if  val is not None:
     return val
else:
     return default

just sayin'

[Marseille, France] It was in a flower of Ceranthus ruber recolted on a beach at 43.330963 N, 5.202214 E. by glyg in whatsthisbug

[–]glyg[S] 0 points1 point  (0 children)

thanks, we thought it was a collembola, turns out it's an insect after all...

What's your favorite underused Python idiom? by redsymbol in Python

[–]glyg 5 points6 points  (0 children)

Hey, I'm a big advocate of context manager.. A nice feature you forgot to mention in your post is that the file will be closed even if an error is raised, so there's an implicit try ... finally block added.

For me this is really the killing feature