by dmarchand90 · 5y ago · 0 comments
The philosophy of python is there should be only one pythonic way of doing things.
But more and more features are being added that give too many options with minor benefit
How is case different from the elif pattern? What does the walrus operator do that we really need.
Each one of these things on their own if fine but I'm worried together it's going to make python increasingly idiosyncratic to each developer and, consequently, harder to read.
Python's Zen beat out perl's TMTOWTDI. But python seems to try to be more and more like perl with every release: more appealing to hackers playing code golf than long term readers of the code base
Pattern matching is often conflated with switch-case by people who are unfamiliar, because pattern matching subsumes switch-case, but they're very much not the same thing. Structural pattern matching is much more powerful than checking a series of boolean conditions. It allows you to not only check the structure of a value, but bind parts of that structure to variables. It allows you to express logic that would would otherwise be fairly complex in a straightforward and transparent way.
> If the implementation is easy to explain, it may be a good idea.
Pattern matching is extremely complex, and for what?
for foo, (bar, baz) in nested_tuple_list:
...
This works as expected. The current 3.10 implementation is just half-baked. They should have expanded the existing pattern matching facilities (added support for data structure destructuring).Presumably then you could just have used the existing walrus operator inside the already existing if-else constructs.
That would have been the obvious path of least surprise. What we got instead seems inadequate.
match command.split():
case ["north"] | ["go", "north"]:
current_room = current_room.neighbor("north")
case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
... # Code for picking up the given object
In to this: if (["north"] | ["go", "north"] := command.split()):
current_room = current_room.neighbor("north")
elif (["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"] := command.split()):
... # Code for picking up the given object
And the walrus operator would assign to 0-many values as required? It kind of breaks it's current meaning and makes the lines very complex, I don't think this version would be more liked...Pattern matching is not (necessarily) extremely complex as it exists in a multitude of languages these days. Powerful does not automatically imply complex.
Anyhow, read the PEP. It is an extremely complex new language feature.
The philosophy of anything (python, unix, etc) is usually just cargo cult early bogus idealism.
What happens later is that reality and pragmatism come into play.
People don't want simple languages, people want more features.
I think it’s a mistake to defer to the zen of python without considering the PEPs themselves which do discuss trade offs. (I’ll note that you didn’t make that claim but GP did).
That’s not a hill I’m willing to die on but I agree with the positives from your list.
And the small handful of times I’ve used walrus have been nice, but not game changing.
Well, then again I wouldn't consider anybody a more expert Pythonista than Guido.
Was it tho? It's one of my favorite improvements, and has no actual downside.
- It violates the longstanding Python rule that assignments don't escape the enclosing scope.
But doesn't that violate the longstanding Python rule that A Foolish Consistency is the Hobgoblin of Little Minds ?
if True: x = 1 print(x)
I get it, languages have to grow or fix mistakes. I read about Python long before I attempted to program in it and even I (not a language designer or that special) knew that their handling of division as being integer division as a default was a long-term problem that would eventually be changed one way or another. And so it was.
I absolutely agree on the walrus operator. It's too terse and while on its own it only makes two steps into one, it's not used alone, making each line of code with it that much harder to unpack.
I don't know enough about pattern matching but my feeling is that we got along without it so far ...
That ship has sailed more than a decade ago.
(And if we're on a crusade to make Python pythonic again, let's start with the bigger fish - like the sync/async function distinction. Somehow Python managed to fall into the red/blue function trap the hardest.)
What should have been done here?
Thiiis really hasn't been my experience with the Python community. Every place I've worked on a Python codebase has done it a different way, and none of them have agreed on what is "pythonic".
Each team has claimed that _their_ approach is the most python-y way to do it, but they've all celebrated Python as not being "restrictive" like "other" languages. It's really weird.
Exactly my experience from day one. It's a laughable statement that the Python community keeps repeated with almost no basis in reality.
With all due respect to your (otherwise perfectly valid) argument, "there's more than one way to do it" was never a real problem with people using Perl. I dare say it's fully possible to write difficult-to-read code in any language.
Perl was expressly never interested in erecting glass walls and force people this or that way. Not saying that this is good or bad. But it's the way the people who are making Perl want it.
Maybe people got into python when Google took it for a serious use just like people got into git because of Linus but figured it wasn't the best tool a decade later but the ecosystem is already there it is too late to swap.
The Zen is against alternatives for the sake of alternatives, but not for facilities focussed on different use cases. Though as the use cases get more finely divided, the difference is less peeceptible.
1.a lot of 'if'-statement 2.using a dict and the value is a function you call if the key matches
Or what else?
That sounds like a desperate plea to get stuck with a bug-riddled unmaintainable mess of a module.
If all you want to do is pick one of a finite set of possible code paths, it is never a good idea to generate a hash to pick from a map/array.
Even chained if/else if statements are cleaner than that mess.
For a not-small example, if you want to switch on an ascii character in a parser, you can make a 256-element list to implement your jump table, and index into it with ord.
If you're performance-minded enough for this to make a lick of sense, you'll take care to only build the jump table once.
And yeah, chained if/else is cleaner because it's the only tool that the language provides. The GP was asking for alternatives. They exist, but you'll hate them.
Granted, you can speed the 256-long if/elif mess up with a binary if/else tree 8 levels deep. But it probably won't beat the jump table.
If you truly find this upsetting, know that it's a practice that results from the language not supporting a switch statement.
Say you have a program like "docker-compose". It has multiple subcommands. Each subcommand is a dict string key and the appropriate action to take is its value, a callable. This also plays well with argparse's parser "choices" argument.
The by far biggest downside is then that all callables mustn't take arguments, or take all the same ones. You cannot dispatch to a callable and dynamically assign what arguments to call it with without losing the prior benefits. Then it gets awkward (like having to throw around args and kwargs all over the place). But up to that point, I find the pattern useful in this case.
Now, every two releases they add a "language feature". Some library maintainers jump in the bandwagon, and WOOSH! : half the ecosystem depends on CPython to run. Bye Jython, Cython, PyPy, Micropython, etc and bye the code analysis and code transformation tools : all outdated until more funds or voluntary dev are willing to fix them
In an ideal world, the community should fork Python 2.7, rename it and add type hints. But that's too late, all the dev time was spent on making libraries compatible only with some recent versions of CPython
https://mobile.twitter.com/vasudevram/status/132736428179752...
Anyway, sick of arguing about such meta points. That is not what I come to HN for. I come for intelligent discourse via which I can learn from others' experience and share mine.
I'll just end by saying that I made specific points in that Twitter thread, such as BDD and RDD influencing the language's direction undesirably. To me, that is specific enough a complaint. A language is much more than just a bag of features. YMMV.
How do the language features not fit? What does "influence undesirably" actually mean? What language features specifically have been added that do not fit, and how have they made the language worse or harder to read or use? That would be an interesting conversation to have.
To my eyes the world does look like a complicated toxic mess. YMMV.