• mint_tamas@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 days ago

    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).

    • gigachad@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 days ago

      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!

      • mint_tamas@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        3 days ago

        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.

        • sugar_in_your_tea@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          2
          ·
          3 days 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.