Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

161–170 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

#161

Earlier quoted context omitted.

Is saving 5 lines of code really worth breaking Python's "one way to do it" design principle?

That principle was bullshit anyway. There have always been multiple ways to do things, the one way was just whatever the elder Pythonista deemed to be pythonic that day.

Not true. The language optimizes for the way it is intended to be used, and changes remain sensitive to those optimizations.

"Pythonic" means intended usage, and "unPythonic" is shorthand for "you found another way to do it that kinda does what you want but (is ten times as slow/takes up ten times as much memory/doesn't work for edge-cases/has more unintentional side-effects) because it wasn't the intended usage, which is fine for your own personal projects, but please don't bring that into my code base, and pretty please don't teach other people to do it that way..."

Re: WTFPython – Understanding Python through surprising snippets

#162
post #8

My biggest complaint about Python is that it somehow doesn’t get flak for having the same (if not worse) scoping as JS, which gets endless hate for its function-scoped variables. (So much so that block scoped variables are the new normal in JS, but not in Py!) Take for instance: >>> powers_of_x = [lambda x: x^i for i in range(10)] >>> [f(2) for f in powers_of_x] [512, 512, 512, 512, 512, 512, 512, 512, 512, 512] To m…

In a decade of python use I can probably count on one hand the number of times lambda has been a good solution to a problem I had. Many times I end up re-writing them as regular functions for clarity sake.

Re: WTFPython – Understanding Python through surprising snippets

#163
post #100

Earlier quoted context omitted.

I don’t think you’ve actually understood my proposal, and the snide remarks about completely unrelated languages don’t help your argument. To reiterate, my proposal is that the walrus operator has the exact same semantics as the assignment operator, but creates an expression rather than a statement. A linter rule could then be added prohibits the assignment operator, and converts all instances of it to the walrus ope…

> and the snide remarks about completely unrelated languages don’t help your argument. Granted, I apologize. It was childish. > but creates an expression rather than a statement. Expressions are limited in Python for the same reasons. It's the same rational that got us tone done lambdas. People code with a certain style using it, which is not the style we want to promote for Python. > A linter rule could then be adde…

> Every time you delegate something to a linter, you fail. A good language must be a good language without 3rd party tooling.

Thank you.

Re: WTFPython – Understanding Python through surprising snippets

#164
post #79
post #77

Earlier quoted context omitted.

The real WTF here is that you are getting a power output from thee ^ operator, when it actually does xor. Indeed, I've never seen JavaScript mix up operators :) /s

HN isn’t able to render two adjacent asterisk, so I had to make do.

If they're surrounded by spaces it goes ok...

    x ** y

Re: WTFPython – Understanding Python through surprising snippets

#165

Earlier quoted context omitted.

> and the snide remarks about completely unrelated languages don’t help your argument. Granted, I apologize. It was childish. > but creates an expression rather than a statement. Expressions are limited in Python for the same reasons. It's the same rational that got us tone done lambdas. People code with a certain style using it, which is not the style we want to promote for Python. > A linter rule could then be adde…

> Every time you delegate something to a linter, you fail. A good language must be a good language without 3rd party tooling. Thank you.

This isn’t delegating anything to the linter, it’s saying that a linter rule would help some people enforce particular styles (only walrus), if that’s what they want. I.e. the point of a linter.

Re: WTFPython – Understanding Python through surprising snippets

#166
post #115

Earlier quoted context omitted.

And then you have to either re-train them to use string cursors or you have lots of people writing inefficient string code.

Not really, no. PyPy demonstrates that.

Sure it does. Where CPython does byte inflation to make sure random access works, PyPy makes you use an extra indexing structure for cases where you can't just advance one char at a time.

I must say it is a rather elegant way, but it still fits firmly within "a new way or inefficient code".

Re: WTFPython – Understanding Python through surprising snippets

#167
post #116
post #80

Earlier quoted context omitted.

Even more fundamental is the nonlocal/global stuff required in order to avoid declaring your variables. Many people are surprised by what this snippet does: a = 0 def f(): print(a) a = 1 f() f()

All it does is produce an error: "UnboundLocalError: local variable 'a' referenced before assignment". The second f() call makes no difference.

Is that what you expected from such a simple piece of code?

Re: WTFPython – Understanding Python through surprising snippets

#168
post #124

Earlier quoted context omitted.

I agree, there is a long list of things I would change in Python. And believe me when I say people involved in the community listen to this very carefully. This is why we had Python 3 in the first place. Because text handling was such a cause of pain. This is why we have type hints in the first place. Because big projects using Python felt let down. And Python 3 and type hints are also a huge source of criticism (I w…

I have a mantra, which this thread engraves so well: People always complain, and developers are the worse If only people cared for fellow human beings, or earth, as much as they care for esoteric Wulrus operations in this or that language.

I suspect it's healthier to judge a few misfeatures in a programming language than to judge all developers, or people as a whole. I'm really very optimistic about the future of humanity, but less so about the future of Python :-)

Re: WTFPython – Understanding Python through surprising snippets

#169
post #168
post #124

Earlier quoted context omitted.

I have a mantra, which this thread engraves so well: People always complain, and developers are the worse If only people cared for fellow human beings, or earth, as much as they care for esoteric Wulrus operations in this or that language.

I suspect it's healthier to judge a few misfeatures in a programming language than to judge all developers, or people as a whole. I'm really very optimistic about the future of humanity, but less so about the future of Python :-)

Fair enough. Make it a longbet - python vs. human race, who's going down first.

Re: WTFPython – Understanding Python through surprising snippets

#170
post #169
post #168

Earlier quoted context omitted.

I suspect it's healthier to judge a few misfeatures in a programming language than to judge all developers, or people as a whole. I'm really very optimistic about the future of humanity, but less so about the future of Python :-)

Fair enough. Make it a longbet - python vs. human race, who's going down first.

I bet neither will go down, but the human race will improve! Seriously, things are getting better - it's just that paying attention to what needs fixing is how engineers contribute to the progress.
Post reply on HN