Live data from Hacker News

WTF Python: Exploring and understanding Python through surprising snippets

github.com

11–20 of 169 posts

Re: WTF Python: Exploring and understanding Python through surprising snippets

#12
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.

Re: WTF Python: Exploring and understanding Python through surprising snippets

#13
post #12

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…

> I've been seeing a growing anti-python sentiment lately

Despite the name, I don't think this is anti-python, in the same way classics like [0] are anti-PHP. The project README mentions this a bit:

> While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Python that you might be unaware of. I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!

0: https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

Re: WTF Python: Exploring and understanding Python through surprising snippets

#14

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

Re: WTF Python: Exploring and understanding Python through surprising snippets

#15
post #12

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…

I agree, it’s the same thing when “WTF JS??” articles always include behaviour that’s part of the floating point spec in their list.

I can’t think of any popular language that doesn’t have weird quirks.

That said, the content of the post is useful. Knowing about this behaviour, even when it’s demonstrated by using language features incorrectly, is important. I’ve seen enough code that misuses “is” on strings to know there’s a need for information like this to be collated and accessible.

Re: WTF Python: Exploring and understanding Python through surprising snippets

#16
post #12

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…

> I've been seeing a growing anti-python sentiment lately Despite the name, I don't think this is anti-python, in the same way classics like [0] are anti-PHP. The project README mentions this a bit: > While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Python that you might be unaware of. I find it a nice way to learn the internals of a pro…

Fair enough I suppose, but I'm still seeing posts like the OP getting linked all over the place by people trying to disparage python as a language.

Of course once you get more deeply into a language it's probably a really good thing to get to know all the weird edge cases. One of my favorite past-times is writing intentionally terrible code just to exercise some of those edge cases. For instance this hello world code uses quite some oddball language features: https://gist.github.com/SuperDoxin/fde4fdad237e85a90c8764cdf...

Re: WTF Python: Exploring and understanding Python through surprising snippets

#17
post #12

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…

I agree with most of your points, because in most cases, people try something very weird, and they are surprised that the result is something even weirder (or they just didn't think through the example logically).

Except the string comparison with "is". It's confusing, because there are no errors, and when I try them in the Python REPL, it "works". The code is logical, I tested the example and it works...

...except when it doesn't, but if I'm just a naive beginner, I'll need to learn the hard way that it doesn't always work

For this reason, I'd be okay with saying that "it" for strings is a WTF of the language.

Re: WTF Python: Exploring and understanding Python through surprising snippets

#18
post #15
post #12

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…

I agree, it’s the same thing when “WTF JS??” articles always include behaviour that’s part of the floating point spec in their list. I can’t think of any popular language that doesn’t have weird quirks. That said, the content of the post is useful. Knowing about this behaviour, even when it’s demonstrated by using language features incorrectly, is important. I’ve seen enough code that misuses “is” on strings to know…

This information is definitely useful, though it probably shouldn't be presented in such a newb-friendly way. It might very well dissuade people from using python based on nonsensical reasons.

Re: WTF Python: Exploring and understanding Python through surprising snippets

#19
post #12

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…

Serious question: how do you know how the language is intended to be used?(or what the one right way is). Last time I checked, I couldn't find a document about this.

Re: WTF Python: Exploring and understanding Python through surprising snippets

#20
post #14

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

It's a design decision to allow it to behave differently, as you can implement += with a more efficient in-place version, instead of a copying one where the lvalue might be different/new and you have to keep the old a.

I even think this is good design, as by convention + does not mutate its arguments but += can be a fast mutating version.

Post reply on HN