WTF Python: Exploring and understanding Python through surprising snippets

  • I feel like WTFs caused by doing something deeply weird hardly counts against the language.

    Yes doing the walrus operator inside brackets works. Yes of course it's going to be doing weird things. Is this really surprising to anyone? It's not what that operator is for.

    Yes comparing strings with "is" works sometimes. It's not checking equality and it isn't supposed to. In another implementation using "is" might work all the time or none of the time. Don't use "is" to compare strings. it's not what that's for.

    Yes chaining mismatching operations does weird things. Be clearer in how you write your code. It's no surprise that writing confusing code results in confusing results.

    I've been seeing a growing anti-python sentiment lately, and most of it seems to be based on these sorts of odd perceived language defects. Python is by no means perfect, but none of the code on this page is something anyone would actually write in production code on purpose.

    I suppose it's a sign of python getting more popular.

  • While this repo is great for education, it doesn't cover the deep problems with Python that you only discover when you try to do something "unapproved" or slightly off the happy path. Depending on the domain you are working in you may never encounter such a situation, but if you do, be prepared to lose your sanity one omnipresent design flaw at a time.

    Over the years I've been collecting a series of cases that are somewhat more nihilistic and depressing under the general category of LOL PYTHON. The little inconsistencies, the boilerplate that is required if you don't want to use only the approved, not performant, and breaks all sorts of other hidden assumptions so you can't actually use the code outside very narrow settings approach, eventually just evoke a nihilistic chuckle and a note to never start a new Python project again.

  • To be frank I think the python community lacks self-awareness.

    There's this ritual you have to do when learning python: blindly accept status-quo design choices as "universally good" (but call it "pythonic"), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (poetry is a good step but not a competitor)), or offer really good IoC tools: just double-down on the status-quo and say something like "looks over engineered, I see no reason why I would need that".

    Pythonic is putting a decorator everywhere, coupling the system together into a ball of mud where you can't decouple it. Pythonic is having annoying mixed naming conventions. Why do I need methods to have underscores in 2021, but classes to use camel case? Why do my lines of code need to fit on a punched card invented decades before the language was invented? Why does the idiomatic "one true way" need to be what some other engineer thinks is "the one true" way?

    Python is a fractal of closed-mindedness and single-use code. There's a reason it took a decade to get projects onto python 3, but PHP can swiftly move the entire ecosystem from v5 to v7 in a year. FastAPI is getting a lot of praise recently, but frameworks of this style are a dime-a-dozen in other languages and have been around for a decade.

    Pythonic is a meaningless word that just gives firepower to the most confident-sounding engineer in a company to shoot down anyone who doesn't code like them. It's not an objective concept, it's innovation poison.

  • Ah yes, the one about mutable default arguments was a real surprise for me once in a debugging session. Python only initialising them once during the whole script runtime is not exactly the behaviour anyone would expect.

  • Python should just remove the `is` operator. If you really need to compare object identities, which is already a rare scenario to begin with, you can explicitly compare the object IDs. Checking if something is or is not True/False/None can just be treating the thing as True-ish or False-ish. Making the language bigger is not nearly as impressive as making it smaller!

  • The Python/NumPy gotcha that has caused me the most pain by far is

      def foo(x, y):
           return x+y
    
    where the intention is for x and y to be NumPy arrays, but the caller accidentally passes lists (or vice versa). This is especially common because a lot of NumPy functions are agnostic as to whether they operate on arrays or other iterables.

  • Compared to wtfjs, I'd say this is pretty tame.

    The only one I actually encountered myself is `def some_func(default_arg=[]):` one, but it was indeed a very "WTF" moment.

  • This is a really impressive list of oddities, with solid explanations on every single one. Excellent resource, thanks!

  •     >>> another_tuple
        ([1, 2], [3, 4], [5, 6, 1000])
        >>> another_tuple[2] += [99, 999]
        TypeError: 'tuple' object does not support item assignment
        >>> another_tuple
        ([1, 2], [3, 4], [5, 6, 1000, 99, 999])
    
    This one's my favorite. Although it's a rare issue that you're unlikely to come across, it's a side-effect of a much bigger design flaw in Python - the decision to make `a += b` behave differently from `a = a + b`.

  • Ah, brings to my mind the classic talk: https://www.destroyallsoftware.com/talks/wat

  •   # Python version 3.8+
    
      >>> a = 6, 9
      >>> a
      (6, 9)
    
      >>> (a := 6, 9)
      (6, 9)
      >>> a
      6
    
      >>> a, b = 6, 9 # Typical unpacking
      >>> a, b
      (6, 9)
      >>> (a, b = 16, 19) # Oops
        File "<stdin>", line 1
          (a, b = 6, 9)
               ^
      SyntaxError: invalid syntax
    
      >>> (a, b := 16, 19) # This prints out a weird 3-tuple
      (6, 16, 19)
    
      >>> a # a is still unchanged?
      6
    
      >>> b
      16
    
    In what way is ANY of this unexpected?

    For starters, unpacking is not the same as assigning inside a tuple...

    (And how is the 3-tuple "weird"? It prints exactly what you did...)

  • all these cool new features! what's the thing they always say about python? "there's more than one way to do it"?

  • If this article was about the WTFs in PHP there would already be hundreds of comments saying how much it sucks.

    You get less criticism about other languages although they also have bad design decisions (which is totally fine since nothing is perfect).

    I wish these things weren't so biased by the emotional investment people put in working tools, which is all a language is.

  • My biggest issue with Python that is not related to syntax is that it isolates you from the Unix machine in a bad way.

    I get that they try to support Windows with the same abstractions, but they are all leaky and subtly broken (like shutil). If you use Python too much, you lose the understanding of Unix.

    Other languages aren't like that, for example Perl and Go.

    Whenever I stop using Python for 2 weeks and focus on C or Go, my understanding of the machine and what is actually going on increases dramatically. I've even considered learning Perl, but haven't done it yet due to lack of time.

  •     >>> a := "wtf_walrus"
        File "<stdin>", line 1
            a := "wtf_walrus"
              ^
        SyntaxError: invalid syntax
    
    Why doesn't this work? Isn't the walrus operator effectively just an alternative to the assignment operator which also returns the assigned value? Why specifically make it fail in this situation?

  • Tips on learning python as a seasoned dev?

    I will start a new job where everything is done in Python in a few weeks. Learning the syntax of the language is easy, but what about "everything else" using a language entails? Like how is stuff written in practice (pythonic / idiomatic), build tools, libs everyone use, workflow, etc

  • Looks like in C++, where the maintainers don't dare to say no to add simple syntax to win popularity contests, but hereby completely destroy the language. JavaScript also comes to my mind.

    walrus, haha

  • TIH python

  • Oh look. Another techie trying to gain cred by hating on things.