Live data from Hacker News

WTF Python: Exploring and understanding Python through surprising snippets

github.com

151–160 of 169 posts

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

#151
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 think most of this is covered by the python rule of thumb "explicit is better than implicit". I have a particularly stubborn cohort who loves nothing better than doing what I call "cargo trains" of combining list comprehensions and "." into sometime 7 or 8+ connected items in one liners. It is ridiculous. I have actually set calendar reminders to go ask him what these trains do after a couple of months between when he wrote the code and $TODAY. It never fails that he'll "get back with me". About an hour later I'll get an explanation via email :) . It never teaches him anything though.

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

#152
post #21
post #5

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

That's a Perl slogan but I suspect you already knew that

yep, corresponding python slogan from PEP-20:

"There should be one-- and preferably only one --obvious way to do it."

this was the compelling argument for python to those who were struggling with large and hard to maintain legacy perl codebases. python was supposed to be a constrained, simple language that encouraged readability- ideally a solution to the problems everyone was facing.

history really does appear to repeat it seems, even in computerdom where the timescales are shorter.

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

#153
post #149

Earlier quoted context omitted.

Oh come on Gary literally runs a top notch teaching site and the link was a bit of fun on his previous teaching site.

Well he also started a spread of snark that made the entire industry look bad. His own site is called destroy all software. Even if that is meant to be ironic, even that irony isn't helpful.

I sincerely think you’re taking Gary and the software industry too seriously. Wat is nowhere near as reputation damaging as, say, “PHP a fractal of bad design”, and PHP is doing great. People have aped the talk to highlight rough corners in their own preferred ecosystems. It’s fun. Programming is dealing with a lot of broken stuff. Embrace it and have a laugh.

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

#154

Earlier quoted context omitted.

Up until mid-2000s, no one had heard of Python. Then there was maybe ten years of “Whitespace? No!” But from say 2012 on there was a big movement of data science and education into Python and its general reputation was quite good. Edit: the XKCD antigravity comic was December 2007, so pretty early on all things considered. PG’s Python Paradox was 2004.

> But from say 2012 on there was a big movement of data science and education into Python and its general reputation was quite good. When Python became popular in education, particularly for first CS courses, is when the public criticism from Scheme, Java, and and C partisans really skyrocketed. Yeah, it has had recently a generally good reputation. That doesn't mean that it has been untouchable and immune to critici…

I’ll agree with that. I think it’s more that the criticisms of the loudest voices has shifted. Once it was “why use an obscure language” then “ugh whitespace” then “dynamic typing is bad” and now more “it’s jumped the shark.”

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

#155
post #97

Earlier quoted context omitted.

Why that exception?

None, True, False are singletons and are never constructed again after initilization of the interpreter (and have been keywords in python2). That's why "is" is always safe. Small integers, as they are kept around, probably are as well (5 is (2+3) works). I think what gets dropped during the conversation is, that we've been living with Java doing it this way (is being == and == being .equals) for decades and nobody re…

I mean why not use == for everything?

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

#156

Earlier quoted context omitted.

They're also an escape hatch, def main(): funcs = [] for x in range(10) def f(x=x): return x funcs.append(f) print([f(x) for f in funcs]) The "x=x" default argument is necessary here. In this example it's just a constant but this trick is often used with mutable defaults.

You're talking about something different. The parent comment is about mutable default arguments are shared between all calls to that function [1], but you seem to be talking about functions defined in for loops all sharing a reference to the same variable [2]. [1] https://github.com/satwikkansal/wtfpython#-beware-of-default... [2] https://github.com/satwikkansal/wtfpython#-loop-variables-le... > In this example it's…

> You're talking about something different.

No I’m not. Exercise for the reader to figure out why. The key thing here is that you want a mutable variable for each closure, but not shared between loop iterations.

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

#157
post #101

Earlier quoted context omitted.

I've observed the weird phenomenon that any valid criticism of Python is treated as either some kind of personal attack or because you're a mad static typing ideologue.

To be honest I feel like most criticisms against any languages are bound to sooner or later be met with the response "well, this is how this language works, and if you don't like it pick another". If you criticize a weakly typed language for an issue that is solved by strong typing, you could even argue that they are correct in saying so, unless you come up with a way to solve it without types. I suspect that the rea…

As an example I recently had someone say that "best practices" are just as good at avoiding the kinds of things a static type system catches. Python seems to be (for whatever reason) a language that creates this kind of ideology with the kind of tropes everyone has heard before.

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

#158
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…

> 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? Yes, it is in fact extremely surprising that an expression does something different based on surrounding context. It would be as if expr had one meaning but it you called f(expr) it had another. Saying that you're using it unexpectedly doesn't really address the issue of langu…

> It is in fact extremely surprising that an expression does something different based on surrounding context

It isn't doing anything different though. Compare:

    >>> a:=1
    1

    >>> (1, 2)
    (1, 2)

    >>> (a:=1, 2)
    (1, 2)
The thing that's confusing people here isn't even the walrus operator, it's tuple syntax.

> If your idea is that an operator is free to do whatever, once it's outside its predicted situation

No it's not. But clearly an operator can never do an operation it isn't defined to do. is and == are different operators and they do different things.

> I think if you think that some of those examples have no legitimate uses at all

They do have legitimate uses, but purposefully obfuscating the context around what the code is doing is hardly the fault of the language.

> I also do not see how this is anti-python, I think you are being unfair to the author.

Yeah on further thought it really isn't anti-python, but articles like the OP do tend to get linked in anti-language posts, which is probably what got me a bit annoyed about the article. No fault of the author.

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

#159
post #87

Earlier quoted context omitted.

Critiquing X, or even warning people about surprises in X, is not "anti-X". Have people picked up this weird idea from fandom culture or something? This article is not saying that people should not use python. It's not criticizing the maintainers for making stupid decisions, as far as I can see. It's just saying "if you do this, you'll get a surprising result", and in the process teaching people more about how it act…

> I've written a lot of C in the subsequent 20 years or so. It has some particularly nasty traps. Acknowledging that does not make me anti-C. depends on your cultural background I guess. Some people see literally everything that is not 100% risk-free as inherently bad.

It doesn't have to be 100% for that to be right.

If we can achieve a N% risk-free Python (without sacrificing other useful features), and we only have a M% risk-free Python (where M < N, or even M << N), then what we have is inherently bad.

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

#160

Earlier quoted context omitted.

> I'm glad it's becoming kind of ok to criticise that language, after years of it being untouchable I've only been following Python for a little over 20 years of it's ~30 year life, but I don't remember it ever being “untouchable”, or even just “untouched” by criticism.

Up until mid-2000s, no one had heard of Python. Then there was maybe ten years of “Whitespace? No!” But from say 2012 on there was a big movement of data science and education into Python and its general reputation was quite good. Edit: the XKCD antigravity comic was December 2007, so pretty early on all things considered. PG’s Python Paradox was 2004.

>Up until mid-2000s, no one had heard of Python

You'd be surprised. Python was used quite a lot in early 00s.

Ruby is the one that was a curiosity few used at that point, until Rails came.

Post reply on HN