Live data from Hacker News

WTF Python: Exploring and understanding Python through surprising snippets

github.com

161–169 of 169 posts

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

#161
post #107

Earlier quoted context omitted.

> I've been seeing a growing anti-python sentiment lately Not speaking for the quality of the article, but on that sentiment: maybe it's just that the pro-python sentiment is eroding. I'm glad it's becoming kind of ok to criticise that language, after years of it being untouchable (at its core, not the 2 -> 3 migration). At least that's my sentiment as someone who really doesn't like it (but I'm a professional, I sti…

You lost me at: “I prefer JS (modern) to Python”. Just... how?

What "how"? Have you modern tried server side JS?

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

#162

Earlier quoted context omitted.

> I've been seeing a growing anti-python sentiment lately Not speaking for the quality of the article, but on that sentiment: maybe it's just that the pro-python sentiment is eroding. I'm glad it's becoming kind of ok to criticise that language, after years of it being untouchable (at its core, not the 2 -> 3 migration). At least that's my sentiment as someone who really doesn't like it (but I'm a professional, I sti…

> JS (modern) LOL. JS is what, 25 years old, versus Python at 30.

LOL, it's about "modern JS", not JS being new.

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

#163

  # 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 "", 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...)

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

#164
post #97

Earlier quoted context omitted.

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?

1. it's a slower check, just like equals() intended for value equality is slower than reference equality == in java.

2. 1 == True and 0 == False, as booleans subclass the int type. On the other hand is distingushes between 1 and True, and 0 and False respectively.

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

#165

Earlier quoted context omitted.

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.

[Edit: deleted my original reply here. I had ended in "Are you sure you didn't misread the original comment?"]

Ah ha! I misread your comment! This was the bit that confused me:

> They're also an escape hatch,

I read this as

> There's also an escape hatch,

So I thought you were saying that the code in your comment was a solution to the problem posted by the original comment. This is why I found your comment so bewildering! I now see that what you were saying that this gotcha (Python evaluates function default arguments, once, at the point of definition) also has a use.

So, I have to say sorry about that. But I also can't help saying your comment could've been phrased a bit less confusingly.

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

#166
post #149

Earlier quoted context omitted.

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.

I did that day I first saw it, and then after three years of people pointing and laughing at how awful computers are for upvotes and likes it stopped being funny.

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

#167
post #166

Earlier quoted context omitted.

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.

I did that day I first saw it, and then after three years of people pointing and laughing at how awful computers are for upvotes and likes it stopped being funny.

They’re not laughing at you, in case you need to hear that. And it’s not computers that they’re laughing at either. It’s human mistakes, misjudgments, and so on. I’m exceptionally talented as a software developer and I make absolutely boneheaded decision, designs and mistakes sometimes. I find it comforting when I just accept my imperfections. I’m more likely to attach an everythingisterrible.gif to my own PR than anything else. It’s made me feel more at ease with myself and my craft to lighten up and have a casual cynicism about the whole thing.

Computers aren’t bad but most of what happens on them is. And that’s okay. Most of everything is bad, in varying degrees. At least the silly bad stuff brings a smile to people’s faces.

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

#168
post #166

Earlier quoted context omitted.

I did that day I first saw it, and then after three years of people pointing and laughing at how awful computers are for upvotes and likes it stopped being funny.

They’re not laughing at you, in case you need to hear that. And it’s not computers that they’re laughing at either. It’s human mistakes, misjudgments, and so on. I’m exceptionally talented as a software developer and I make absolutely boneheaded decision, designs and mistakes sometimes. I find it comforting when I just accept my imperfections. I’m more likely to attach an everythingisterrible.gif to my own PR than an…

No I totally get this. But the cynicism has been relentless since this online, and contagious, and many use it as an in joke, and this is serious stuff. People's careers... 6 hour conference calls involving tens of experts trying to get to the bottom of these things.

Most of these things are just people doing things outside of the language spec and then are somehow surprised when it doesn't work and then make a joke of it all and I don't see how that helps anything.

Again, it was funny at the beginning, but now I really wonder if these people just want to hurt other people by being abusive with this incessant snark. Perhaps this an Eternal September issue, so maybe you're somewhat new, but shitting on things is easy.

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

#169
post #58

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.

I disagree.

The intention in foo is not for x and y to be numpy arrays, it is to add 2 objects together. "If it walks like a duck and it quacks like a duck, then it must be a duck".

That function can work on anything implementing +, this is the philosophy of Python. It enables the whole numpy, dask, TensorFlow ecosystem.

If you intend to limit it to numpy arrays, you either use numpy 1.20's static types, or you add more checks at the beginning of the function, such as np.asarray(x).

Post reply on HN