[HN Gopher] WTFPython: Exploring and understanding Python throug...
___________________________________________________________________
WTFPython: Exploring and understanding Python through surprising
snippets
Author : Tomte
Score : 207 points
Date : 2023-08-27 11:46 UTC (11 hours ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| Pannoniae wrote:
| I would find this funny, if not each and every one of these
| represents a potential bug in software. Some are really esoteric
| (like the name mangling example) but many are something you might
| do accidentally.
|
| 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.
| stevesimmons wrote:
| It's a huge leap to conclude "for greenfield projects ... using
| Python is not necessarily a very good idea".
| Pannoniae wrote:
| It's certainly a leap... but is it wrong though? You might
| conclude either "yes" or "no", but dismissing it outright is
| I don't think very conductive to a good conversation.
| nerdponx wrote:
| Unjustified leaps tend to be wrong. And yes it's wrong
| because if you apply the criterion consistently then you
| will end up never choosing any language or tooling ever
| because everything has some kind of problem.
| Pannoniae wrote:
| Sure, but you can choose the one which is the most
| suitable with the least amount of problems. I would argue
| that Python has many problems, so it's suitable for a
| fairly small set of use-cases. (by design, btw)
| asicsp wrote:
| Some of the substantial previous discussions:
|
| * 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)_
| bandyaboot wrote:
| I had forgotten about the walrus operator. I'm curious what the
| rationale is for not just allowing the regular assignment
| operator within expressions for that feature. Would it confuse
| the interpreter or something?
| saiojd wrote:
| I would guess, to make sure the classic noob mistake of typing
| `if x = 3:` instead of `if x == 3:` stays a syntax error
| maweki wrote:
| Depending on what you do on the lhs of = you can actually
| overwrite this operator.
|
| 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.
| assimpleaspossi wrote:
| [flagged]
| di4na wrote:
| One can. Everyone can.
|
| It happens that words have meaning and using "vulgarities" have
| its place and meaning too.
|
| You may not agree, language is a dynamic thing, but even in a
| technical or professional settings, these words have their use.
| assimpleaspossi wrote:
| Being vulgar for the sake of being vulgar is childish.
| di4na wrote:
| You are the one saying it is for the sake of being vulgar
| and not to convey a necessary strong emotion.
|
| You can choose to cut yourself from this part of your
| humanity. That is your choice. But don't scowl at others as
| childish because they have real emotions to express.
| assimpleaspossi wrote:
| One can express strong emotion without vulgarities in
| every language in multiple ways.
| di4na wrote:
| And there are also some emotions for which vulgarities
| are the right choice
| uxp8u61q wrote:
| > Please don't post shallow dismissals, especially of other
| people's work. A good critical comment teaches us something.
|
| > Please don't pick the most provocative thing in an article or
| post to complain about in the thread. Find something
| interesting to respond to instead.
| TrickardRixx wrote:
| The license suggests you can do as you please with the project.
| Feel free to rewrite it to conform to your own sensibilities.
| Pannoniae wrote:
| Conversely, vulgarities drive your point across stronger (given
| that you aren't using them every second sentence) Compare: "the
| project is being late" with "the project is getting fucking
| late", for example.
|
| I don't really get the self-censorship though. Either don't use
| vulgarities, or actually use them, not this self-bowdlerising
| stuff.
| watchyoprofmity wrote:
| [flagged]
| crawsome wrote:
| It has 33k stars and has been for for 6 years. Sounds like
| people don't care.
| [deleted]
| kristianp wrote:
| One python library WTF I came across recently is the autojunk
| parameter of the difflib SequenceMatcher class. Despite passing
| constructing the class with
| 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...
| bjoli wrote:
| This is drives home some of the things I really like about
| scheme. Scheme is built on a few selected primitives and much of
| the language comes together on top of that.
|
| 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.
| Lyngbakr wrote:
| Are you using Scheme in production? I'm super curious about it
| being applied in the "real world", as I'm currently working my
| way through SICP.
| ak_111 wrote:
| No way. Scheme is hobbyist language at this point.
|
| 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.
| agumonkey wrote:
| Very nice website, I love these counterintuitive yet non absurd
| examples.
| 8589934591 wrote:
| Wow. Can't believe a couple of these are in my production. Esp
| with the mixture of py2 and py3. And people who are not well
| versed in python are just not aware of these gotchas.
|
| 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.
| jackblemming wrote:
| Consider not holding strong beliefs without empirical evidence.
| There are both plenty of long term python projects that have
| held up, and no strong evidence one way or the other on type
| safety.
| 8589934591 wrote:
| There are both merits and demerits to python. Unfortunately
| not everyone has the opportunity or the luxury to work with
| projects that are properly maintained. Even messy python
| projects in legacy companies hold up but that does not mean
| quality and maintenance costs are good. My beliefs stem from
| my experience with the jobs available in my area and country.
| I'm not the expert on type safety but my understanding from
| various sources and my own experience is that type safety is
| superior. Go has been the best candidate as the alternative
| for the projects I have undertaken. YMMV.
| oivey wrote:
| For most of these type hints or static typing do not at all
| help. I haven't went through all of them, but in the first
| quarter or so almost all of them were due to leaky compiler
| weirdness in CPython.
| synergy20 wrote:
| I agree, there should be a book specifically made for python-
| wtfs, e.g. corner cases, practical hacks, name it as 'python
| traps and pitfalls'
| moralestapia wrote:
| Lol.
|
| 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".
| legends2k wrote:
| All languages have quirks. It's the degree of WTF-ness and how
| much they affect majority of its users that it becomes popular
| as a WTF-y language.
|
| 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?
| thrdbndndn wrote:
| To me, it's about how often you encounter these quirks when use
| the language "intuitively" (very subjective, I know).
|
| 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.
| moralestapia wrote:
| I disagree and think is the other way around.
|
| 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.
| thrdbndndn wrote:
| Really? To me most of JS quirks are things like `(1 == '1')
| == true`. Or why there are both `for .. of` and `for ..
| in`. Maybe you're just too familiar with the language to
| forgot about these things.
| moralestapia wrote:
| Just use '===' and all these "quirks" go away. Or learn
| the proper semantics for false-ish/true-ish (takes about
| 20 mins.) and you're on the other side.
|
| 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.
| [deleted]
| meatjuice wrote:
| It's destroying my stupid brain but interesting for sure.
| Timothycquinn wrote:
| Love this. I've written a ton of Python in recent years but never
| used the 'is' operator. After reading this I'm glad I did not.
|
| I can see usecases but clearly it should be used sparingly.
| wzdd wrote:
| To be honest this response underscores the problem with pages
| like this. In Python, strings and numbers are objects, and "is"
| tells you if they are the same object. You wouldn't compare
| strings or numbers in C using a pointer comparison, and you
| shouldn't do it in Python either. The fact that it works
| sometimes in cpython is a coincidence.
|
| It's interesting to learn about how the interpreter is
| implemented, but that's about it.
| kstrauser wrote:
| You should absolutely be using `is` where appropriate. `x is
| None` is almost always preferable to `x == None`. If you're
| checking for object identity, use `is`. If equality, use `==`.
| They're different use cases.
| maweki wrote:
| Especially since == can be overwritten, while 'is' can not.
| kstrauser wrote:
| That's right. Any class can define its own __eq__ method.
| wodenokoto wrote:
| How do you check for None?
| stevesimmons wrote:
| Or check that two dicts really are the same object, as
| opposed to two different dict objects that just happen to
| have the same keys/values?
| wodenokoto wrote:
| I'm not sure I've ever had to do that. When is that a need?
| misnome wrote:
| My immediate first thought is, optimisation? If you know
| you've been the same object, you could skip e.g.
| comparison, or change update logic
| dist-epoch wrote:
| This kind of micro optimizations don't make much sense in
| Python. They complicate the code, and you are still 100
| times slower than compiled languages.
| thrdbndndn wrote:
| I personally do use "if var is None:", but can't you just use
| "if var == None"?
| nomel wrote:
| 'is' checks if it the object ids are the same, with None
| having a unique one. Equals can be tricked.
|
| Here's a class that is equal to None, and everything else:
| class EqualsEverything: def __equals__(self,
| other: Any) -> bool: return True
| thrdbndndn wrote:
| Thanks, I saw this on SO too.
|
| Just curious: would it ever happen in practice?
| svilen_dobrev wrote:
| every little thing happens in practice... usually
| unnoticed and buried while refactoring something
| innocent/ly.
|
| 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..
| JoBrad wrote:
| How is that your response, after reading this article? The is
| operator checks whether the two items are the same object,
| which is critical in some circumstances.
| yoyopa wrote:
| shit like this is why anyone calling themselves a software
| engineer should have to get a license and formally sign and seal
| their code
| h2odragon wrote:
| Python's "int" optimization of pre-allocating objects for values
| between -5 and 256 can be a great source of fun when writing
| extensions, too.
|
| 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_.
| jokoon wrote:
| I still have the feeling that those oddities are still a bit less
| bad than the ones of js.
|
| 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.
| moralestapia wrote:
| >I still have the feeling that those oddities are still a bit
| less bad than the ones of js.
|
| I'm truly interested in hearing why does that matter?
| [deleted]
| PicassoCTs wrote:
| At least its not lua. My personal wtf was with lua ignoring
| variable scope.. its just a hashtable it all goes into it -
| https://onecompiler.com/lua/3zjsw5up3
| spacechild1 wrote:
| I'm not sure what your code example is supposed to
| demonstrate... All I can say is that Lua uses lexical scoping
| for local variables (which need to be declared as "local").
| synergy20 wrote:
| where does it ignore the scope? use 'local' it stays in the
| function scope, without 'local' it's global, other scripting
| languages(e.g. bash) does the same.
| raytopia wrote:
| I'm overall not super happy with python's game ecosystem too
| (especially the lack of gui based editors/engines) but if
| you're willing to look past the lack of an editor Panda3D is a
| pretty good/robust game engine for Python. Disney used it for
| their ToonTown and Pirates MMO along with a lot of themepark
| rides (visualiztion/actually using it in the ride) so it's
| pretty mature. It's a lot more flexible then Godot and you get
| access to the entire Python ecosystem.
___________________________________________________________________
(page generated 2023-08-27 23:00 UTC)