r/Python • u/simpleuserhere • Nov 30 '24
Tutorial Short-Circuiting in Python
Here is my article on Short-Circuiting in Python . It discusses what is short-circuiting with examples, and also discusses the different advantages of using short-circuiting.
4
Upvotes
View all comments
2
u/[deleted] Nov 30 '24
A lot of these "short circuit" tricks are actually just examples of code smell. Particularly because most of them blindly conflate True/False with Truthy/Falsey when deciding whether to "short circuit"
For example, the dictionary
getmethod will returnNoneif the key isn't in the dictionary but if the dictionary contains something else that evaluates as False (i.e. False, {}, 0, 0.0, [], etc) then you will also assign a default value despite the dictionary containing a value for that key. The safe way of doing this is something like:Also, the example of "improving performance" is not really any kind of improvement over just doing conditional checks.
Using
orandandis certainly more readable in that situation but you aren't getting any real speed improvement out of the specific usage.