this post was submitted on 12 Apr 2025
195 points (90.1% liked)

Technology

69109 readers
2819 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] sugar_in_your_tea@sh.itjust.works 6 points 1 week ago* (last edited 1 week ago)

Yes, Python is the wrong choice if performance is your top priority.

But here's another perspective: why leave easy performance wins on the table? Especially if the cost is simpler code that works as you probably wanted anyway with both None and []?

Python is great if you want a really fast development cycle, because the code is generally quite simple and it's "fast enough." Any wins for "fast enough" is appreciated, because it delays me needing to actually look into little performance issues. It's pretty easy for me to write a simple regex to fix this cose (s/if len\((\w+)\) == 0:/if not \1:/), and my codebase will be slightly faster. That's awesome! I could even write up a quick pylint or ruff rule to catch these cases for developers going forward (if there isn't one already).

If I'm actively tweaking things in my Python code to get a little better performance, you're right, I should probably just use something else (writing a native module is probably a better use of time). But the author isn't arguing that you should do that, just that, in this case, if not foo is preferred over if len(foo) == 0 for technical reasons, and I'll add that it makes a ton of sense for readability reasons as well.

Here are some other simple wins:

  • [] and {} instead of list() and dict() - the former copy constants, whereas the latter actually constructs things; oh, and you save a few chars
  • use list comprehensions instead of regular loops - list comprehensions seem to be faster due to not needing to call append (and less code)
  • use built-ins when you can - they're often implemented in native code

I consider each of those cleaner Python code anyway, because they're less code, just as explicit, and use built-in language features instead of reinventing the wheel.