Live data from Hacker News

WTFPython – Understanding Python through surprising snippets

github.com

101–110 of 199 posts

Re: WTFPython – Understanding Python through surprising snippets

#101
post #100

Earlier quoted context omitted.

> Why should people only use it when necessary? Because we have experience with things such as easy of learning, cognitive load, writing simple to read code, making it hard to introduce bugs... This comment show how little experience one can have with it, and yet make a quick judgment to offer to "fix" things. E.G: > eel free to use it in all places you’d previously use the assignment operator.”. Simple, easy to unde…

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…

> the actual creator of the language was so against the addition of this operator that he saw the community’s insistence on it as reason to step down as BDFL.

That's backwards. The community was generally opposed to the walrus operator, and Guido stepped down because of (among other things) the community's reaction when he insisted on adding it.

Re: WTFPython – Understanding Python through surprising snippets

#102
post #82

Earlier quoted context omitted.

> there should be one-- and preferably only one --obvious way to do it. Yes, this has been one of the hardest balance to find. And believe me when I tell you the community tries very, very hard. But it's a difficult problem: making the language evolves fast enough, but keeping it solid and stable. Very difficult indeed. > Python became a language that can be really hard to read now. Not in my experience. My job invol…

The new walrus operator now makes indentation obsolete. You can write averything within one line as an array and it is hard to read. This in combination with syntactic sugar, operator overloading and unicode variables can make the language very hard to read. e.g. # compute pi 1000000 >> ψ( ψ(χ>>op("(x**2+y**2)**0.5 >Σ*4>> _/_) or this: # 10 fibonacci numbers [x:=[1,1]] + [x := [x[1], sum(x)] for i in range(10)] is va…

> [x:=[1,1]] + [x := [x[1], sum(x)] for i in range(10)]

Tried to run this in a REPL and got invalid syntax.

Re: WTFPython – Understanding Python through surprising snippets

#103
post #73

Earlier quoted context omitted.

As OP noted, given the existence of type(a).__iadd__[0] `a += b` essentially desugars to: `a = a.__iadd__(b)`. list.__iadd__ is (sadly) defined as something like def __iadd__(self, other): self.extend(other) return self So it's possible to have __iadd__ itself succeed modifying the list in place but then the "no-op" reassignment fail. [0] like many data model operations there's really a bunch of fallbacks depending o…

I'd almost consider this a subtle bug? None of the other += operators returns a value.

> I'd almost consider this a subtle bug?

It's not exactly a bug but it is a somewhat unexpected behaviour and IIRC Guido regretted that list.__iadd__ was overridden this way.

> None of the other += operators returns a value.

It's not the operator which returns a value, it's the data model hook. The data model requires that it return a value: https://docs.python.org/3/reference/datamodel.html?#object._...

> These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self).

Really the issue is that the "in-place" hooks are simply weird.

Re: WTFPython – Understanding Python through surprising snippets

#104

Earlier quoted context omitted.

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

What do you think each(), map() and filter() do ? But even without that, I'm not saying they are comparable. I'm saying the same API will use explicit anonymous callbacks in JS and something else in Python (decorators, subclassing, protocols, generators...). I'm saying that the same API will use __iter__ in Python and something else in JS (type conversion, proxy object, explicit method call...). E.G, this is a Python…

Map, filter, and fold are higher order functions, functions which take functions as parameters.

You are misusing the word callback. A callback is a function passed to another thread that will maybe be invoked later as a response (like it calls you back).

Re: WTFPython – Understanding Python through surprising snippets

#105
post #100

Earlier quoted context omitted.

> Why should people only use it when necessary? Because we have experience with things such as easy of learning, cognitive load, writing simple to read code, making it hard to introduce bugs... This comment show how little experience one can have with it, and yet make a quick judgment to offer to "fix" things. E.G: > eel free to use it in all places you’d previously use the assignment operator.”. Simple, easy to unde…

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 added prohibits the assignment operator, and converts all instances of it to the walrus operator.

Every time you delegate something to a linter, you fail. A good language must be a good language without 3rd party tooling. Tooling should only be a bonus. This is why we have forced indentation in Python. This is why we have namespaces and don't rely on a bundler.

> What the language creators have done instead is add a whole new operator with all new semantics, leading to all new sources of confusion. (See TFA). This is not easy to learn.

You don't learn this operator when you learn Python. Just like people don't learn about list comprehensions at first, don't use yield and code without creating a decorators for years.

Python scales down: you are productive with it without all its features. It is designed to be partially learned and still be useful. Thousands of Python devs are not coders, but scientists, geographers, finance people, etc. They know very little Python and yet, can work all day with it.

Walrus is made so that it doesn't interfere with the original design. It's something separate that you get to later.

> yet ignore the fact that the actual creator of the language was so against the addition of this operator that he saw the community’s insistence on it as reason to step down as BDFL.

That's not what happened. The bad quality of the debate is what made Guido step down. We had a huge number of new comers in the later years because of Python popularity. These people didn't learn internet from the age of mailing list, IRC, etc. They addressed the topic like others talk on Twitter: lot of noise, little content. I understand that after 30 years of doing what is basically free awesome work for thousands of people, he didn't feel like being disrespected by a horde of juniors that though they knew better.

Re: WTFPython – Understanding Python through surprising snippets

#106
post #96

Python 3 supports typing. The integration with VSCode is also nice. The following is valid typed code and my personal favorite WTF: def func() -> int: return True Because: https://github.com/satwikkansal/wtfpython/blob/master/README... I understand the decision that was made in the python 2 era. Nowadays Boolean shouldn't be a subtype of `int`. It goes against the zen of python: - Explicit is better than implicit. It…

Yeah, typing KINDA works until you try to use a library that has no annotations and uses reflections, such as - say - plumbum, boto3 or some other pre-typing shit. It's a bit like with async code - once you decide to use it, you can keep the language but basically need a whole new ecosystem. I tried to write an actual company project with `mypy --strict` passing as a requirement and you quickly end up having abstract…

Yeah, agreeing with what you say.

mypy --strict is only useful for projects for which everything is defined.

I recently did a project with tornado, which is typed. Typing helped me immensely. My editor would give me type hints, errors and show me other edge cases.

For an existing Django project I'm adding types when I touch functions. It helps a bit, but way less. That's also because typing almost forces you to change all the dicts that get's passed around in dataclasses. Which is a lot of work after in an existing project.

Re: WTFPython – Understanding Python through surprising snippets

#107

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.

I've coded in python for >10 years and C# for ~half of that. I prefer indentation-based control flow purely because it condenses code vertically and creates consistent indentation with actual purpose (many of the devs on C# projects I've dealt with have had inconsistent tab/space settings and no linting). If anything, the one thing I hate about Python3 is dynamic typing - inferring datatypes in function bodies is gre…

Hmm, I'm not really a fan of indentation based control flow, but I see the appeal of condensing code vertically.

I primarily work with C#, but I've gradually come to like the "K&R"/Javascript style (where the 1st brace is at the end of the first line), precisely because it helps condense code vertically.

Re: WTFPython – Understanding Python through surprising snippets

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

> the actual creator of the language was so against the addition of this operator that he saw the community’s insistence on it as reason to step down as BDFL. That's backwards. The community was generally opposed to the walrus operator, and Guido stepped down because of (among other things) the community's reaction when he insisted on adding it.

Oh interesting. I’m with the community on this one.

Re: WTFPython – Understanding Python through surprising snippets

#109
post #70

Earlier quoted context omitted.

It uses unicode but it doesn’t use UTF-8 by default as external encoding and never as internal encoding except for an optional secondary buffer.

What do you mean by "external encoding", "internal encoding", "secondary buffer"?

Encoding strings internally as UTF-8 is a bad idea, since you can't do efficient constant time access (utf-8 isn't fixed width).

Re: WTFPython – Understanding Python through surprising snippets

#110
post #82

Earlier quoted context omitted.

The new walrus operator now makes indentation obsolete. You can write averything within one line as an array and it is hard to read. This in combination with syntactic sugar, operator overloading and unicode variables can make the language very hard to read. e.g. # compute pi 1000000 >> ψ( ψ(χ>>op("(x**2+y**2)**0.5 >Σ*4>> _/_) or this: # 10 fibonacci numbers [x:=[1,1]] + [x := [x[1], sum(x)] for i in range(10)] is va…

> [x:=[1,1]] + [x := [x[1], sum(x)] for i in range(10)] Tried to run this in a REPL and got invalid syntax.

python 3.8? try:

https://tio.run/#python38pr

Post reply on HN