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".

Software developer. Java, Python, C++ etc. I write for pythoninformer.com and maintain the generativepy library.

Software developer. Java, Python, C++ etc. I write for pythoninformer.com and maintain the generativepy library.