• Kissaki@programming.dev
    link
    fedilink
    English
    arrow-up
    6
    ·
    8 hours ago

    While the 2nd approach is not wrong, the first method is considered more Pythonic. Many people don’t agree, but I’ve already put forward my points in a previous article on that debate.

    Does Pythonic mean best practice in the python community or “good python”?

    If “many people don’t agree”, how can they claim it to be “pythonic”? Isn’t that contradictory?

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      4 hours ago

      “Many” isn’t the same as “most,” though I don’t think there’s any way to know what “most” is.

      But here’s my reason for agreeing w/ the OP:

      • not x checks both None and emptiness, which is usually desired
      • len(x) == 0 will raise an exception if x is null
      • with type hinting, those should be the only two reasonable options

      It’s nice that it’s slightly faster, though performance has absolutely nothing to do w/ my preference, though it does have something to do with my preference for avoiding exceptions for non-exceptional cases.