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

Technology

69041 readers
5540 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
[–] [email protected] 2 points 1 week ago (1 children)

Apart from the quote from the zen of python, does this really make your code better though? You will end up writing 4-5 lines with an extra level of indentation. The code does the same, but has worse performance and communicates the intent poorly (compared to the “pythonic” version).

[–] [email protected] 1 points 1 week ago (1 children)

I am not saying it's better, just that I don't like the proposed way :) I would argue that being "pythonic" has even less value than the Zen, which I quoted because it's true, not because it is some strict rule (which it isn't anyway).

You could argue I also need to write that extra code for the if not case, as I explicitly have to check if it is None if my program somewhere further down expects only lists.

Hunting for those sweet milliseconds is a popular game in the Python community ;) if this mechanism is that important for your program, you should definitely use it, I would do as well!

[–] [email protected] 2 points 1 week ago (1 children)

I think pythonic is more important than performance and I would still choose that version over a try-catch block, were it slower. Being pythonic means it represents a commonly understood pattern in Python code, therefore it is more efficient in communicating intent.

[–] [email protected] 2 points 1 week ago

Exactly. The point of following a code style is to make obvious patterns easy to spot and deviations stand out. That's why code style guidelines say your priorities should be:

  1. follow whatever style the code around it uses
  2. follow project style guidelines
  3. do the technically optimal option

3 should only be prioritized if the win is big enough, and there should probably be a comment right there explaining why the deviation was made.