omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    The Little Book of Python Anti-Patterns

    Pythonista
    3
    4
    2276
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ccc
      ccc last edited by

      A nice online resource of Python worst practices http://docs.quantifiedcode.com/python-anti-patterns They also have a free automated tool that you can point at any public GitHub repo to find where code might be suboptimal.

      A podcast with the authors http://www.talkpythontome.com/episodes/show/18/python-anti-patterns-and-other-mistakes

      Yet another example of Python innovation coming from Berlin ;-)

      1 Reply Last reply Reply Quote 1
      • dgelessus
        dgelessus last edited by dgelessus

        I somehow doubt the knowledge of whoever wrote the first page... Most of the suggestions were alright, but a few things were just wrong:

        • For if conditions it recommends using if cond is True or if cond is False, which is just as wrong as if cond == True and if cond == False that it recommends against.
        • It also mentions if cond and if not cond, but says that those only work if cond is a boolean object, i. e. True or False, which is not true at all - this version works with any object, because all objects have a truth value (usually True), unlike the is test, which only works with True and False exactly, not with other objects with a truth value.
        • It pretends that is and is not behave like isinstance and talks about writing if rectangle is Circle, which is wrong. This misunderstanding might explain the misconception about if statement conditions.
        1 Reply Last reply Reply Quote 1
        • ccc
          ccc last edited by ccc

          I agree with your comments but the pages are still a great resource along the lines of the Jeff Knupp's book Writing Idiomatic Python.

          I avoid using is altogether because of the blog post Why you should almost never use is in Python. I use if cond and if not cond almost everywhere for all data types because I find it more readable and less error prone. I therefore try to avoid comparing variables directly to True, False, None, '', [], {}, etc in alignment with Jeff Knupp's 5.1.4 Avoid comparing directly to True, False, or None.

          1 Reply Last reply Reply Quote 0
          • cneumann81
            cneumann81 last edited by

            Thanks for pointing this out. It's fixed. Feel free to put any other things you might find into our issue tracker or just open a pull request. Any input is highly appreciated.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors