Is try-except unpythonic? Surely duck typing (which is pretty much as pythonic as it gets) relies on catching exceptions?

Using get with a default is great if you need to handle missing keys by substituting a default value, which you sometimes do.

But in other cases, looking up a missing key would indicate a serious error condition, which is exactly what exceptions are for. So you should use [] and try-except.

Square brackets are not a "mistake".

Written by

I am a software developer with over 30 years experience in Java, Python and C++. I write for pythoninformer.com.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store