|
| ▲ | sevensor 18 hours ago | parent | next [-] |
| Last 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. |
|
| ▲ | agumonkey 21 hours ago | parent | prev | next [-] |
| I thought I knew enough about python culture but TIL https://realpython.com/python-lbyl-vs-eafp/#errors-and-excep... |
|
| ▲ | andy99 17 hours ago | parent | prev | next [-] |
| to me something like for key in possible_keys:
if key in collection:
...
is fine and isn’t subject to your disadvantage. |
|
| ▲ | IshKebab 20 hours ago | parent | prev [-] |
| You should do normally do data = collection.get("key")
if data is not None:
...
else:
....
|
| |
| ▲ | japhyr 20 hours ago | parent | next [-] | | Wouldn't this be a little cleaner? data = collection.get("key")
if data:
...
else:
...
| | |
| ▲ | pansa2 20 hours ago | parent | next [-] | | If 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. | | | |
| ▲ | blackbear_ 19 hours ago | parent | prev | next [-] | | No, 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. | |
| ▲ | Shish2k 20 hours ago | parent | prev | next [-] | | That behaves differently (eg if collection["key"] = 0) | |
| ▲ | IshKebab 17 hours ago | parent | prev | next [-] | | No, 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. | |
| ▲ | tayo42 20 hours ago | parent | prev [-] | | it depends on what's in the if blocks |
| |
| ▲ | echoangle 19 hours ago | parent | prev [-] | | The value in the collection could be the actual value None, that’s different from the collection not having the key. | | |
|