WTFPython: Exploring and understanding Python through surprising snippets
github.com
github.com
* https://news.ycombinator.com/item?id=31566031 (460 points | May 31, 2022 | 139 comments)
* https://news.ycombinator.com/item?id=26097732 (359 points | Feb 11, 2021 | 162 comments)
* https://news.ycombinator.com/item?id=21862073 (396 points | Dec 23, 2019 | 185 comments)
Since learning that I have started disliking all the things that I just have to accept in other languages. I can accept the c++ variant of "because this and that comes together like this. Because speed", but the pythonesque "because because" drives me insane.
Simply put difference between a 'hobbyist' and professional language for me is the strength and depth of the packaging ecosystem.
So as soon as you need to do things like open and parse PDF, implement TLS/SSL, create a dashboard,... you will be in trouble pretty quick in scheme. Probably simple stuff like handling unicode would be shaky.
I did write a web app that served a couple of hundred people with information, chats and live updates at a conference a friend was hosting. It was written in guile and made heavy use of delimited continuations which is probably my favourite way of handling state on the web. The main logic was less than 2000 lines, so it was by no means a large project.
What about the unicode handling would be shaky? And why do you say handling unicode is simple?? :)
Good repo, I learnt quite a few more... wtfs. :)
I have a strong belief that unless type hints, testing, tooling is leveraged properly, python does not qualify as a good candidate for long term projects. Go handles all of this automatically and I'm having good results with it as an alternative to Python for small to medium scale projects.
I like putting in types just to help my IDE with autocomplete, though. I have also caught a few errors with mypy that could have caused a crash in very unlikely cases. I'm not convinced it's worth rigourously typing a project. It seems like mostly a nerd snipe because I've noticed there is a satisfaction with getting in right despite not making any difference to the user.
difflib.SequenceMatcher(isjunk=None, ...
The matcher will start classifying characters as junk once the b parameter is more than 200 characters long. So unless you read the docs carefully, your matcher will start not matching anything longer than a few characters once the input gets longer than a few sentences. The trick is to also set autojunk=False in the constructor.Its like the original writer of this component wrote the matcher for a specific application (DNA matching?) and left in the heuristic even though it isn't applicable to general use. At some point they added the autojunk parameter to gate this behaviour, but it still defaults to true, to keep backwards compat, and to confuse people.
https://docs.python.org/3/library/difflib.html#difflib.Seque...
Same mechanism, overwriting the value stored in the allocated object; but when the extension is in loose C code which is casting pointers with abandon... things can go wrong.
It could be setattr or setitem instead of bind-to-name. Because you can overwrite = only in certain cases that are defined, by the syntax, you can't easily elevate = to an expression and be done with it.
I can see usecases but clearly it should be used sparingly.
It’s interesting to learn about how the interpreter is implemented, but that’s about it.
Also, you are somewhat changing the topic from “what is an example of when you might want to is-compare two dicts”, no?
Here’s a class that is equal to None, and everything else:
class EqualsEverything:
def __equals__(self, other: Any) -> bool:
return TrueJust curious: would it ever happen in practice?
sooner or later the __eq__ method will be redefined for some class, then reworked, and then.. == None might not be what was supposed to be..
or, my favorite, x='a' ; (x,)[0] == x[0] == x .. but are only equal until x changes to something not-1-long-sequence..
So, I use "is" since "is" is not a context dependent concept, like equals is. I've seen this once in the wild, and it made sense for its use:
def __eq__(self, other):
return bool(self) == bool(other)Although I have written over a hundred thousand of lines of code in Python over the years; I use Python mostly for dev ops tooling, reporting, monitoring and automation so they don't get super complex and they mostly can lean on procedural programming patterns.
I could imagine complex frameworks needing heavy use of Objects that could lean on the 'is' keyword.
If you look at the argument list you don’t know what will be passed by value and what by reference. You will have to guess their types first. And that is risky.
Sadly, there are no easy paths to fix this because compatibility (https://xkcd.com/1172/), but for greenfield projects which aren't expected to be small throwaway projects, using Python is not necessarily a very good idea.
I am a still disappointed by python because I am so addicted to all the fun things of python, yet python is inadequate for game development.
I use godot, which has a python-flavor language, but it's missing A LOT of what I love about python: list comprehension, tuple, set, and many others. And now that I think about it, it's going to be difficult for them to evolve the language, although I often prefer to break codebases.
I'm truly interested in hearing why does that matter?
i'm doing some whataboutism, sure
Talk about setting the bar low.
Funny how when JS is involved the frame is "WTF, this is why JS is a shit language and no one should use it".
Whereas with python is "WTF, Python is great and I don't understand it well enough".
I encountered dozens of "JS WTF" when learning without actively trying weird things. It's ultimately on me for not understanding the language better, no argument here, but it feels unintuitive.
And for Python, while I agree with most of cases listed in the repo to be indeed WTF (and a very good resource to learn it deeper!), I don't really encounter most of them naturally, other than the implicit string literal concatenation and default mutable arguments.
The TFA shows several examples of code that someone learning the language would definitely hit. Most of the time JS "quirks" are due to code that is so complicated that the actual WTF is on why would someone design such code in the first place.
The Python ones are abhorrent, here's a few of them:
* the 'is' operator behaving differently, even when called with operands of the exact same type?
* (from TFA) # This will print True or False depending on where you're invoking it (python shell / ipython / as a script) (WHAT?!)
* No multi-line lambdas because that makes the AST unparseable, literally. Then cover it up with some shit argument about how ackchually is more "pythonic" to only use one line functions, lol.
The more the users the more chances of this too.
Personally I find Python to be ergonomic and I've been less bitten by it. But you might get a totally different opinion if the same question is asked to a different group. This is a Python post so I guess majority would be pro-Python?