Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

41–50 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

#41
post #27

Earlier quoted context omitted.

I agree totally with that, it's an ugly trap in python, although mitigated by the fact the language doesn't encourage this kind of paradigm so you rarely encounter it. But more absurd than creating a global variable if you forget "var/let/const" ? More absurd than "this" being schizophrenic ? More absurd than having no namespace for 20 years ? That's pushing it.

In my opinion, if standard tooling is able to identify the foot-gun, it’s not too terrible of a foot-gun. I don’t think standard python tooling would catch the above issues, but standard JS tooling would identify a missing variable declaration. As for the other things you mention (this, namespaces), I never said JS was perfect! Just that JS got a lot of shit for its scoping, fixed the problem, and now has sane scopes…

> I don’t think standard python tooling would catch the above issues,

Of course it does.

    $ echo "
    > powers_of_x = [lambda x: x^i for i in range(10)]
    > [f(2) for f in powers_of_x]
    > " > test.py
    $ pylint test.py
    ...
    test.py:2:27: W0640: Cell variable i defined in loop (cell-var-from-loop)
    ...
Pylint is the most powerful linter for Python is well integrated in VSCode or PyCharm.

When concerns are raised about JS, people are quick to answer you are not supposed to use it without modern tooling (Webpack + babel + typescript + eslint + lowdash...), although many devs still do.

Now, I use and teach a lot of JS, and I enjoy many of its modern features. I especially like the object destructuring, it's even better than Python's unpacking.

But objectively, you can get a very decent experience in Python without any kind of tooling. And yet, there is a lot of it if you want to up your game: pylint, black, mypy, jedi, pytest, poetry, etc.

Re: WTFPython – Understanding Python through surprising snippets

#42

It's interesting to see the walrus operator in there. I wonder, is this another example of unnecessary features being added just to claim a bigger change list, or someone really needs this operator? Why would anyone prefer a walrus instead of adding just one more line to their code, which also helps with readability?

This has been subject to endless discussions already and it's even a factor in Guido's decision to quit his role of BDFL.

Re: WTFPython – Understanding Python through surprising snippets

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

I don't find this at all unexpected. In an imperative language I expect a loop to be implemented by mutating a common variable.

Do you perhaps think that because you come from a background in low-level languages?

Re: WTFPython – Understanding Python through surprising snippets

#44
post #38
post #18

Earlier quoted context omitted.

Maybe I’m misunderstanding, but I’d argue that arrow syntax in js is equally special syntax as lambda function. They’re both native ways of making anonymous functions. Python’s version is more clunky, but no more special

Interesting, I don’t use python day-to-day, so I had assumed that the delayed capturing of the iteration variable was a lambda thing. Looks like this is still broken: for i in range(10): def mul(x): return i^x powers_of_x.append(mul) That to me coming from a JS background is totally wild

It’s actually due to python late binding. The variables are looked up at call time, rather than function define time. One could argue that your example shouldn’t work anymore in python3 since variables in list comprehensions go out of scope when they finish now. I haven’t tried it myself. I fill it under, “things I never need to do” :-)

Re: WTFPython – Understanding Python through surprising snippets

#45

It's interesting to see the walrus operator in there. I wonder, is this another example of unnecessary features being added just to claim a bigger change list, or someone really needs this operator? Why would anyone prefer a walrus instead of adding just one more line to their code, which also helps with readability?

A while ago, I was also sceptical about the walrus operator, although I use assignments in expressions all the time in C++. But when I was perusing a library written Python the other day, I found five spots in which the walrus operator would make sense - eliminating one line of source code without readability suffering from it.

Re: WTFPython – Understanding Python through surprising snippets

#46
post #28

Earlier quoted context omitted.

I agree totally with that, it's an ugly trap in python, although mitigated by the fact the language doesn't encourage this kind of paradigm so you rarely encounter it. But more absurd than creating a global variable if you forget "var/let/const" ? More absurd than "this" being schizophrenic ? More absurd than having no namespace for 20 years ? That's pushing it.

Modules avoid the variable issue because ‘use strict’ is applied by default for them. IMO JS as a language is in a much better place than Python currently. I end up frustrated a fair amount by arcane errors in Python due to less expressive constructs or some wtf issues like unexpected variable scoping within Python modules.

This is most probably because you are trying to code in Python like you code in JS: this always leads to frustration.

I started to have fun coding in JS the day I accepted it was not Python and that I had to structure and style my code differently.

E.G: in Python you will use iteration a lot. A lot of a lot. But not so many callbacks. The reverse is true in JS.

Re: WTFPython – Understanding Python through surprising snippets

#47
post #27

Earlier quoted context omitted.

In my opinion, if standard tooling is able to identify the foot-gun, it’s not too terrible of a foot-gun. I don’t think standard python tooling would catch the above issues, but standard JS tooling would identify a missing variable declaration. As for the other things you mention (this, namespaces), I never said JS was perfect! Just that JS got a lot of shit for its scoping, fixed the problem, and now has sane scopes…

> I don’t think standard python tooling would catch the above issues, Of course it does. $ echo " > powers_of_x = [lambda x: x^i for i in range(10)] > [f(2) for f in powers_of_x] > " > test.py $ pylint test.py ... test.py:2:27: W0640: Cell variable i defined in loop (cell-var-from-loop) ... Pylint is the most powerful linter for Python is well integrated in VSCode or PyCharm. When concerns are raised about JS, people…

That’s good to hear. As I said, I don’t use python much anymore.

Re: WTFPython – Understanding Python through surprising snippets

#48
post #45

It's interesting to see the walrus operator in there. I wonder, is this another example of unnecessary features being added just to claim a bigger change list, or someone really needs this operator? Why would anyone prefer a walrus instead of adding just one more line to their code, which also helps with readability?

A while ago, I was also sceptical about the walrus operator, although I use assignments in expressions all the time in C++. But when I was perusing a library written Python the other day, I found five spots in which the walrus operator would make sense - eliminating one line of source code without readability suffering from it.

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

Re: WTFPython – Understanding Python through surprising snippets

#49
post #30
post #15

Earlier quoted context omitted.

I find the explanation lacking? sort also "changes the list in-place", but you can do ([3, 1, 2],)[0].sort()

Of course you can. The first element of that tuple is a list, which is mutable, and can be sorted in-place.

Pls read again https://github.com/satwikkansal/wtfpython#-mutating-the-immu...

Re: WTFPython – Understanding Python through surprising snippets

#50
post #25

Earlier quoted context omitted.

I agree totally with that, it's an ugly trap in python, although mitigated by the fact the language doesn't encourage this kind of paradigm so you rarely encounter it. But more absurd than creating a global variable if you forget "var/let/const" ? More absurd than "this" being schizophrenic ? More absurd than having no namespace for 20 years ? That's pushing it.

After a long time using both languages, I’d argue that non-sane equality checks in js is the biggest source of bugs.

Unfortunately, this is not true. Although the equality operators are a favorite bikeshedding topic, the largest source of errors in JS code bases are type errors. Using a "non-sane" equality check, such as those with type coercion would actually mask or alleviate these source of bugs you mention.
Post reply on HN