Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

31–40 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

#31
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 as, or confused for, the operator for equality or assignation.

We, as a community, didn't want people to wonder why there are several ways to do assignation (we have this problem with string templating already). Instead, we wanted to be sure that people could ignore the existence of ":=" for some times during their learning process.

And so the decision has been taken to make is very easy to distinguish it from "=" and "==", forcing parenthesis when necessary to make it clear this is a completely different use case. Also to hint people at using it only when necessary.

If you like scripting in Python because it's so easy to go from an idea to code, it's not random luck. It's because the language is a collections of thousands of such decisions.

Re: WTFPython – Understanding Python through surprising snippets

#32
post #18
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…

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

Aren't Python's lambdas expressions while JS's arrow functions are not?

Re: WTFPython – Understanding Python through surprising snippets

#33

If only it had curly braces instead of just indention. God, that kills me. And used unicode instead of ascii as the default. And there wasn't python 2.7 vs 3. Someone help me stop this list.

Braces in Python would have issues, e.g. ambiguity with set/dict literals. Most similar languages (except JavaScript) seem to use begin/end instead. Would that be more acceptable than significant indentation?

Re: WTFPython – Understanding Python through surprising snippets

#34
post #32
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

Aren't Python's lambdas expressions while JS's arrow functions are not?

Arrow functions are expressions in JS.

Re: WTFPython – Understanding Python through surprising snippets

#35
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.

Re: WTFPython – Understanding Python through surprising snippets

#36

If only it had curly braces instead of just indention. God, that kills me. And used unicode instead of ascii as the default. And there wasn't python 2.7 vs 3. Someone help me stop this list.

When you write in a language which uses curly braces for statement grouping, do you indent your code? ...? I thought so.

Re: WTFPython – Understanding Python through surprising snippets

#37
This list is a little nit-picky.

All the id() equivalencies only exist for optimization, and not out of some promise to the user.

The walrus operator can be confusing, but any language can be made to look confusing. It's an advanced feature that should be used very scarcely (if at all).

It does point out a few real weak points in Python, but most of them require non-trivial usage of the language.

Meanwhile, in Javascript

    > "a" + undefined
    "aundefined"
Now that's what real WTF looks like.

Re: WTFPython – Understanding Python through surprising snippets

#38
post #18
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…

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

Re: WTFPython – Understanding Python through surprising snippets

#39
post #15
post #6

the first really wtf thing on there is https://github.com/satwikkansal/wtfpython/blob/master/README... where both effect and exception happen; up to that point it's business as usual with garden variety corner cases or implementation details.

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.

Re: WTFPython – Understanding Python through surprising snippets

#40
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.

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 same).

The exception occurs at step 2, but this step is otherwise a no-op. Whether mutating a reference to the same value it already had is an actual mutation is an interesting semantics debate.

Post reply on HN