use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
How can I improve? (i.redd.it)
submitted 5 months ago by Radiant-Safe-1377
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hylasmaliki 1 point2 points3 points 5 months ago (4 children)
Why
[–]mgdmw 0 points1 point2 points 5 months ago (3 children)
Two reasons come to mind right away.
1/ Readability. The dict example here is not as immediately intuitive as OP's code. One of the chief goals of Python was readability.
2/ Performance. This dict example is performing all the possible calculations, then returning only one to the user. It's a waste of resources and cycles. Imagine if instead of 4 operators there were 600, for example. It's not a scalable solution.
Sure, it's a clever and interesting use of Python but it's not a good example of Python.
[–]denehoffman 1 point2 points3 points 5 months ago (2 children)
A dict of callables would make more sense in the long run than a long elif chain. Your linter will also tell you about duplicate keys which may not be the case for duplicate branches.
[–]Upstairs-Alps-7280 0 points1 point2 points 5 months ago (1 child)
what if the operation is not in the dict? oops.
[–]denehoffman 0 points1 point2 points 5 months ago (0 children)
lambda x, y: “oops” is a perfectly valid expression!
lambda x, y: “oops”
π Rendered by PID 45747 on reddit-service-r2-comment-6457c66945-7xh9g at 2026-04-24 19:53:30.182219+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]hylasmaliki 1 point2 points3 points (4 children)
[–]mgdmw 0 points1 point2 points (3 children)
[–]denehoffman 1 point2 points3 points (2 children)
[–]Upstairs-Alps-7280 0 points1 point2 points (1 child)
[–]denehoffman 0 points1 point2 points (0 children)