4 ms·
All iterators in Python use exceptions for flow control, as do all context managers for the abort/rollback case, and it is generally considered Pythonic to use
by formerly_proven 10mo ago
All iterators in Python use exceptions for flow control, as do all context managers for the abort/rollback case, and it is generally considered Pythonic to use single-indexing (EAFP) instead of check-then-get (LBYL) - generally with indexing and KeyError though and less commonly with attribute access and AttributeError.
[heavy green check mark]
try:
data = collection['key']
except KeyError:
data = ..try something else..
[red x]
if 'key' in collection:
data = collection['key']
else:
data = ..try something else..
The latter form also has the genuine disadvantage that nothing ensures the two keys are the same. I've seen typos there somewhat often in code reviews.
- agumonkey 10mo agoI thought I knew enough about python culture but TIL https://realpython.com/python-lbyl-vs-eafp/#errors-and-exceptional-situations-preventing-or-handling-them https://realpython.com/python-lbyl-vs-eafp/#errors-and-excep...
- IshKebab 10mo agoYou should do normally do data = collection.get("key") if data is not None: ... else: ....
- japhyr 10mo agoWouldn't this be a little cleaner? data = collection.get("key") if data: ... else: ...
- tayo42 10mo agoit depends on what's in the if blocks
- Shish2k 10mo agoThat behaves differently (eg if collection["key"] = 0)
- pansa2 10mo agoIf valid `data` can be zero, an empty string, or anything else “falsy”, then your version won’t handle those values correctly. It treats them the same as `None`, i.e. not found.
- japhyr 10mo ago:facepalm:
- blackbear_ 10mo agoNo, this would crash with numpy arrays, pandas series and such, with a ValueError: The truth value of an array with more than one element is ambiguous.
- IshKebab 10mo agoNo, truthiness (implicit bool coercion) is another thing you should avoid. This will do weird things if data is a string or a list or whatever.
- echoangle 10mo agoThe value in the collection could be the actual value None, that’s different from the collection not having the key.
- sevensor 10mo agoLast time I measured it, handling KeyError was also significantly faster than checking with “key in collection.” Also, as I was surprised to discover, Python threads are preemptively scheduled, GIL notwithstanding, so it’s possible for the key to be gone from the dictionary by the time you use it, even if it was there when you checked it. Although if you’re creating a situation where this is a problem, you probably have bigger issues.
- andy99 10mo agoto me something like for key in possible_keys: if key in collection: ... is fine and isn’t subject to your disadvantage.