Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

51–60 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

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

Are they really foot-guns? The lambda variable reference thing has appeared more in forums comparing languages than I’ve seen it used at all in python and I’ve spent a lot of time working in large open source python projects with thousands of contributors.

Even if you got the binding right, your powers of 2 would be shot down for being unpythonic anyway due to lambdas being shunned when there are cleaner ways to write things.

powers_of_x makes no sense as a variable name for a list of functions that you have to call in order with the same number to actually get the powers of that number. I get that it’s just a contrived example, but most can be turned into someone more apparent to the reader.

    def powers_of_x(x, limit=10):
        for i in range(limit):
            yield x^i
Less memory, no anonymous functions, more flexibility, testable...

Re: WTFPython – Understanding Python through surprising snippets

#52

It's a good repo, but remember a lot of those are "A Good Thing™". The first snippet is a very good example: a := "wtf_walrus" doesn't work while: (a := "wtf_walrus") works. It's a fantastic design decision. Python took a long time before getting this operator, because it's a language that favors being readable, easy to use, and above all, to learn. But in many other languages, the very same operator is often misused…

[deleted]

Re: WTFPython – Understanding Python through surprising snippets

#53
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()

`a += b` does something along the lines of `a = a.__iadd__(b)`. Here `__iadd__` performs the actual append, but then the assignment fails.

Does it though? So why doesn't a += b change the object id id(a) ?

Re: WTFPython – Understanding Python through surprising snippets

#54
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

Why would it be "totally wild" coming from a JS background when the corresponding JS code:

    for(var i=0; i i^x);
    }
behaves the exact same way for the same reason?

By definition, a closures closes over its definition context, it doesn't capture the values at definition time but instead keeps referring to said definition context. If the definition context is mutable and modified, the closure reflects that change when it's finally invoked.

Re: WTFPython – Understanding Python through surprising snippets

#55

It's a good repo, but remember a lot of those are "A Good Thing™". The first snippet is a very good example: a := "wtf_walrus" doesn't work while: (a := "wtf_walrus") works. It's a fantastic design decision. Python took a long time before getting this operator, because it's a language that favors being readable, easy to use, and above all, to learn. But in many other languages, the very same operator is often misused…

in the zen of python (import this) it says:

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

sadly this is not true for a while in python now. Python became a language that can be really hard to read now.

Re: WTFPython – Understanding Python through surprising snippets

#56
post #53

Earlier quoted context omitted.

`a += b` does something along the lines of `a = a.__iadd__(b)`. Here `__iadd__` performs the actual append, but then the assignment fails.

Does it though? So why doesn't a += b change the object id id(a) ?

Because if `a` and `b` are lists, `a.__iadd__(b)` mutates `a` in place, then returns it.

Re: WTFPython – Understanding Python through surprising snippets

#57
post #25

Earlier quoted context omitted.

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.

I was thinking more of the case where the types match but objects and arrays with the same contents are considered different. I’ve watched every member of my team get stung by it again and again - and then have to create workarounds to get past it.

Re: WTFPython – Understanding Python through surprising snippets

#58
post #30

Earlier quoted context omitted.

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

So the question is if += and sort are both "in-place", why does one throw and the other doesn't? Clearly "in-place" is not the full explanation here. I think the real explanation is that with += there are two steps: 1- The existing list gets modified. This is fine since lists are mutable. 2- The tuple's reference to the list gets updated (even though this update is unnecessary since the list object's identity is the…

Tuples are immutable, but that doesn't mean they can't contain references to mutable objects.

When you want to use a tuple as a key in a dictionary, Python checks if all members of the tuple are immutable. If one or more of them aren't, you'll get an 'unhashable type' error.

Re: WTFPython – Understanding Python through surprising snippets

#59
post #28

Earlier quoted context omitted.

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.

Iterators and callbacks aren’t comparable. You don’t use them for the same thing.

Re: WTFPython – Understanding Python through surprising snippets

#60
post #45

Earlier quoted context omitted.

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?

Yes. That principle was quite silly to begin with.
Post reply on HN