« BackWhat the Fuck Pythoncolab.research.google.comSubmitted by sundarurfriend 2 days ago
  • harrisi a day ago

    I'm surprised by the reactions to this. It's made immediately clear in the notebook that this is a fun look at some interesting behaviors of Python. There's no implication that there are bugs - I have no idea where anyone got that idea - except for the single actual bug in CPython that is mentioned (https://github.com/python/cpython/issues/54753).

    I don't feel strongly about Python, but I do think CPython is a great way for people to learn a bit about how interpreted languages work. The code is quite easy to understand, and you can easily play with these interesting aspects of how languages and runtimes work. Does every Python programmer need to know the difference between `a = 256` and `a = 257`? No. Is it interesting? Yes. Should someone on your team know? Probably.

    There's a lot of interesting stuff here. Understanding the difference between the conceptual idea of each line of code and what actually happens is fun, and, in some cases, important.

    • kazinator a day ago

      Bad requirements are bugs.

      Bugs in understanding. Bugs in good taste.

      A roach in your soup is a bug, even if the recipe says it should be there.

      • harrisi a day ago

        I think I know what you mean, but there's a line somewhere. Just because everyone doesn't understand the intricacies of the micro ops their cpu is using or every type of cache involved in various situations doesn't mean their computer is full of bugs.

        Similarly, as long as the mental model of a Python programmer is in line with the results of executing some computation with Python, all is well.

      • msgodel a day ago

        It probably wasn't a good idea to start it out is showing string constants aren't optimized. If they wanted to talk about really unintuitive reference gotchas initialization in optional function parameters would have been a way better example.

        • harrisi a day ago

          I appreciate the sentiment, but even this is a good example of why this stuff is interesting. Because some strings _are_ optimized (using some definition of "optimized"), but only in some cases.

          It's these quirky little things where the code doesn't exactly represent the way execution happens in most straightforward mental models of how execution works. Again, it's not bad and it's not unique to Python. It's just the nitty gritty of how some things work.

          I will say that calling it "What The Fuck Python" is certainly more attention-grabbing than "Some Interesting Bits About Python Internals You Probably Don't Need To Know". What're you gonna do. :)

          • msgodel a day ago

            I don't know I think "Python doesn't optimize <x>" is one of the least surprising classes of statements in programming.

      • Danjoe4 a day ago

        Who writes python this way? Good Python code, being such a flexible language, relies on conventions. Never seen the id() function in production code. "Is" is canonically used for "if var is None", not string comparisons. Python has a rich library of string methods, maybe use them?

        You're finding bugs because you're using the language like a QA tester instead of the way its intended. If I saw string_var is "some_string" in a PR, I would reject it. Even the triple boolean comparison example should just be separated with an "and".

        Read PEP20 and PEP8. A goal of Python is to be as readable as possible, not to conform to some spec. You may not agree with that design decision, but all these examples have an obviously more readable alternative which would work.

        Jeez. It's like me complaining that Rust looks ugly (it does). The language was designed with a different set of priorities.

        • duncanfwalker a day ago

          I mainly see this wtf pages as a hook to talk about the internals of a language/interpretor/etc. As you say, there's no point reading them as a criticism of the language because that's just not what production python looks like. I read the line of code and think 'can I imagine why it does that?' if I can I skip the explanation but sometimes I have absolutely no idea what's going on and they're the ones I like to read the detail on

          • crazygringo a day ago

            > A goal of Python is to be as readable as possible, not to conform to some spec.

            The goal of every programming language should be to conform to a spec that is as logical and consistent as possible. This prevents bugs by saving tons of mental effort having to worry about and look up edge cases, and not always catching them all.

            I don't mind so much if end-user applications have bugs when they're used differently from intended, because their use cases are narrow. If Word crashes when I put 10,000 images on a single page, well obviously it's not built for that.

            But I mind very much when programming languages have bugs, because they are the layer on top of which we build so many other things! The use cases for a programming language are almost infinitely wide.

            For a language as widely used as Python, we should be testing it out like QA testers. All these inconsistencies add up to tons of bugs when you expect something to follow a consistent pattern and it doesn't.

            • evertedsphere a day ago

              > While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Python that you might be unaware of. I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!

              the spirit in which that page is presented is different from what you seem to have taken it to mean

              • coldtea a day ago

                You seem to have missed the entire purpose of the post.

                It's not to give "best practices" but to unveil gotchas and peculiarities in the language implementation and internals.

                >A goal of Python is to be as readable as possible, not to conform to some spec.

                This doesn't even make sense.

                • motorest a day ago

                  > You're finding bugs because you're using the language like a QA tester instead of the way its intended.

                  What do you think this means? Are the bugs there or not?

                  • FreakLegion a day ago

                    Which ones are bugs? I read the first few sections and glanced through the rest, but it's a long notebook. There were runtime-specific implementation details, operator precedence, genuine language footguns (like scoping rules), but no bugs that I saw.

                    • fragmede a day ago

                      Not all bugs are equal though, and if I'm only going to run across the bug when I'm standing on my head the third Wednesday of the month while singing Poker Face, it's a bit less concerning than one that happens only on days that end in "Y".

                      • happytoexplain a day ago

                        So we shouldn't learn about them? They're offensive to enumerate? No, they're useful to understand.

                        • motorest a day ago

                          I think you're showing some despair in trying to avoid addressing the fact that there are indeed bugs. There is nothing suggesting bugs are obscure or random. You have a list of examples. Is it hard to acknowledge the problems are there? What exactly makes you feel threatened or personally attacked by a bug tracker?

                      • sundarurfriend a day ago

                        > Who writes python this way?

                        People learning the language or learning programming, something Python is supposed to be good for and is widely used for. Also, casual and intermittent users of Python, who might not hold all the details in their head because they work in multiple different languages. These categories would encapsulate the majority of Python users in practice, and all of them end up "QA testing" the language unintentionally and wasting time and effort figuring out inscrutable problems.

                        A better designed language could prevent a lot of these abuses, give informative error messages, or at the least warnings. These are not inevitable results of language flexibility, but in many cases just the result of poor design that got patched up imperfectly over time - which is understandable given Python's history, but doesn't make the problems go away.

                        • happytoexplain a day ago

                          Why are you being so defensive? Every language has bad or surprising (which IMHO is a synonym for bad) design choices. These are just a few of Python's. "You're using it wrong" seems like an unrelated criticism. Languages have the luxury of dictating how you use them.

                          • undefined a day ago
                            [deleted]
                            • pyuser583 a day ago

                              “Is” checks memory location of the values.

                              • constantcrying a day ago

                                >Who writes python this way?

                                No one, obviously. This is about understanding how the python interpreter functions.

                                >Python has a rich library of string methods, maybe use them?

                                How are you able to get the impression that any of this is a prescription on how python programming should be done? Nowhere does it claim to be, in fact if you read the actual content you would understand why these examples aren't the way to accomplish your goals.

                                Why are you so incredibly defensive when someone points out that a programming language contains weird behavior most people do not understand? This is true for every language. And almost always these examples are given so that people can refine their understanding of the language.

                                >Jeez. It's like me complaining that Rust looks ugly (it does). The language was designed with a different set of priorities.

                                The only person complaining is you.

                              • alabhyajindal a day ago

                                Preach. We need more Python slander. It annoys me to no end when people trash JavaScript as if it was some exception in bad language design. Most languages have something bad that can be picked up and trashed on - some more than others.

                                • bnchrch a day ago

                                  Thank you! For years Ive been saying the sheer volume and fury by which Javascript is criticized is much more applicable to Python.

                                  Both aren't perfect languages, and both are far from "the best" but it is maddening that Python get often picked as a darling when its implementation, ecosystem and tooling are fundamentally at odds with correctness and productivity.

                                  • verbify a day ago

                                    I think people pick on js more because it's the only option for scripting frontend for the web, while if you don't like python there's probably another language that can be used instead.

                                    • socalgal2 a day ago

                                      I'd say people pick on JS because they don't learn it. They jump in, use it like python or C++, get frustrated it's not the same as what they're used to, vow to use it as little as possible. This was especially true before es5/es6 with var function scope and no class syntax.

                                      • dreamcompiler a day ago

                                        Unless you're doing AI, in which case you're royally screwed.

                                      • 0xDEAFBEAD a day ago

                                        The author of this notebook does not appear to agree with you. They describe Python in the beginning as "beautifully designed".

                                        This is not idiomatic Python code. It's code designed to demonstrate Python performance optimizations, through use of relatively obscure Python functionality which exposes details of the internals in situations where you need that.

                                        "its implementation, ecosystem and tooling are fundamentally at odds with correctness and productivity"

                                        You'll need a lot more than just this notebook to argue for that position.

                                      • hamstergene a day ago

                                        There could be generational gap here: for many people the first JS was ES5 or later, which is after the language was taken seriously and fixed. The difference between Python2 and ES1-ES3 was much bigger than between modern Python and modern JavaScript, so some protest against old bad fame is understandable.

                                        But, if you have any suspicion that JavaScript has been treated unfairly: it hasn't. It is literally being replaced by TypeScript as we speak. The fact you have to invite people to come up with more Python slander is itself a testimony, JavaScript has never needed an invitation. Having lot more issues than others is what being exception means, and JavaScript is one.

                                        • motorest a day ago

                                          > We need more Python slander. It annoys me to no end when people trash JavaScript as if it was some exception in bad language design.

                                          Python's file system support across platforms is notoriously problematic or even outright broken. Wasn't this enough reason to put it lower in the quality bar than JavaScript?

                                          • akdor1154 a day ago

                                            JavaScript doesn’t even have file system support.. each runtime has its own tacked on implementation in the relevant “standard” library. Now which is notoriously problematic?

                                          • heavenlyblue a day ago

                                            Given that it starts with a thing called "id" which is an implementation detail and probably should not be used for equality, it seems like this is a geniuinely well designed language :)

                                            • mirashii a day ago

                                              id is not an implementation detail, it's a core built-in function of the language. The value that it returns is an implementation detail, but the properties of the value it returns are not and perfectly valid to rely on.

                                              • zahlman a day ago

                                                `id()` is documented and mandated to exist, and the result provides guarantees that make it suitable for identity (not equality) checks[1] (although of course you should normally use the `is` operator). Pitfalls involving it are generally really pitfalls with understanding object lifetimes[2].

                                                [1]: https://docs.python.org/3/library/functions.html#id

                                                [2]: see e.g. https://stackoverflow.com/questions/52096582

                                              • throwaway314155 a day ago

                                                JavaScript is far more jarring than Python depending on your background. They do both have issues of course, but that's the reason JavaScript gets "picked on" more.

                                                • fragmede a day ago
                                                  • bb88 a day ago

                                                    I came here to say that. That was an eye opening intro to Javascript for me.

                                                  • moffkalast a day ago

                                                    Yeah python deserves a good bashing as well, it's about as bad as JS in this regard. I mean for bool("false") resolves to True ffs.

                                                    • 3eb7988a1663 a day ago

                                                      That is entirely consistent with how truthiness works in Python. The function does not claim to parse strings, that is how you get YAML Norway.

                                                      Do you think there should be different results for bool("no"), bool("false"), bool("heckno!"), bool("heckyes!")?

                                                      Edit: should have included internationalization: bool("nein!")

                                                      • bnchrch a day ago

                                                        Don't forget its transitive dependencies creating non-deterministic builds

                                                        • librasteve a day ago

                                                          yeah

                                                          in raku, i checked

                                                            say False.so; #False
                                                        • bb88 a day ago

                                                          How does this attract more people to hackernews who come from the python world? If anything, it just shows how toxic people can be on this site.

                                                        • hansvm a day ago

                                                          The first third of this seems to just be complaining that it's not obvious when two objects are actually the same object, and that if you mistake identity-related operators for other operators then you'll have a bad day.

                                                          That's a fair critique. It's a little weird that `is` and friends have dedicated, short, nice syntax.

                                                          On the other hand, most compiled languages are compatible with an implementation which admits the following optimization:

                                                            const a: u32 = 42;
                                                            const b: u32 = 42;
                                                            assert(&a == &b);
                                                          
                                                          The language usually doesn't guarantee that different immutable variables have dedicated memory on the stack, or that they even live on the stack in the first place.

                                                          That's the same class of error we're seeing here, and even among popular interpreted languages Python is by no means unique. I might get to the bottom 2/3 of the doc later, but the rest was off-putting enough that I don't know I'll bother.

                                                          • hmry a day ago

                                                            Yeah, it would be much clearer if instead of an `a is b` operator, there was a `same_object(a, b)` built-in method.

                                                            • bb88 a day ago

                                                              It doesn't matter much since pointers are never comparable in python, just values.

                                                          • pansa2 a day ago

                                                            My favourite Python misfeature is that `x += y` sometimes does the same thing as `x = x + y` and sometimes doesn't. This leads to minor WTFs like this:

                                                              >>> a = b = (1, 2)        >>> a = b = (1, 2)
                                                              >>> a = a + (3, 4)        >>> a += (3, 4)
                                                              >>> b                     >>> b
                                                              (1, 2)                    (1, 2)
                                                            
                                                              >>> a = b = [1, 2]        >>> a = b = [1, 2]
                                                              >>> a = a + [3, 4]        >>> a += [3, 4]
                                                              >>> b                     >>> b
                                                              [1, 2]                    [1, 2, 3, 4]
                                                            
                                                            And major ones like this:

                                                              >>> a = ([1, 2], ['one', 'two'])
                                                              >>> a[0] += [3, 4]
                                                              TypeError: 'tuple' object does not support item assignment
                                                              >>> a
                                                              ([1, 2, 3, 4], ['one', 'two'])
                                                            
                                                            The operation raises an exception even though it succeeds!
                                                            • GeneralMayhem a day ago

                                                              Your first example has to do with the fact that tuples are copied by value, whereas lists are "copied" by reference. This is a special case of an even larger (IMO) misfeature, which is that the language tries very, very hard to hide the concept of a pointer from you. This is a rampant problem in memory-managed languages; Java has similar weirdness (although it's at least a bit more consistent since there are fewer primitives), and Go is doubly odd because it does have a user-controllable value vs. pointer distinction but then hides it in a lot of cases (with the . operator working through pointers, and anything to do with interfaces).

                                                              I think the whole thing does a misservice to novice or unwary programmers. It's supposed to be easier to use because you "don't have to worry about it" - but you really, really do. If you're not familiar with most of these details, it's way too easy to wander into code that behaves incorrectly.

                                                              • throwaway2037 21 hours ago

                                                                    > This is a special case of an even larger (IMO) misfeature, which is that the language tries very, very hard to hide the concept of a pointer from you.
                                                                
                                                                When I came to Python from Perl, it only took me about one day of Python programming to realize that Python does not have references the same way that Perl does. This is not flame bait. Example early questions that I had: (1) How do create a reference to a string to pass to a function? (2) How do I create a reference to reference? In the end, I settled on using list of size one to accomplish the same. I use a similar trick in Java, but an array of size one. In hindsight, it is probably much easier for junior programmers to understand the vale and type system in Python compared to Perl. (Don't even get me started about the readability of Perl.) Does anyone still remember the 'bless' keyword in Perl to create a class? That was utterly bizarre to me coming from C++!
                                                                • pansa2 a day ago

                                                                  > Your first example has to do with the fact that tuples are copied by value, whereas lists are "copied" by reference.

                                                                  My mental model for Python is that everything is '"copied" by reference', but that some things are immutable and others are mutable.

                                                                  I believe that's equivalent to immutable objects being 'copied by value' and mutable ones being '"copied" by reference', but "everything is by reference" more accurately reflects the language's implementation.

                                                                • dchftcs a day ago

                                                                  I'd argue the first one is not a smaller WTF than the latter. It's a silent failure that also mutates state, arguably worse than getting a crash.

                                                                  With that said, a = b = [1,2] itself is a bit of a code smell, but I don't take issue with it here because it can arise in more subtle ways sometimes.

                                                                  • pansa2 a day ago

                                                                    > I'd argue the first one is not a smaller WTF than the latter.

                                                                    I think the first one probably arises more frequently, but at least there's a fairly straightforward explanation: "`x += y` acts in-place if-and-only-if `x` is mutable".

                                                                    The second example is more niche but it's much harder to explain what's going on - it can only be understood by first understanding the first example, and what it implies about the implementation of `+=`.

                                                                • sundarurfriend a day ago

                                                                  Aside: In the process of submitting this, I learnt that Hacker News apparently has an anti-censorship title transform built-in. I submitted it with the title taken from the notebook - "What the f*ck Python!" - and HN automatically removed the ! and changed "f*ck" to "Fuck".

                                                                  • LPisGood a day ago

                                                                    Some of these just seem to be using Python out of spec and being surprised that implementation details exist, and misunderstanding boolean expressions.

                                                                    • otabdeveloper4 a day ago

                                                                      Python doesn't have a spec. It barely even has documentation nowadays. (Sad. Twenty five years ago it was the gold standard of documenting software.)

                                                                      • zahlman a day ago

                                                                        Python's documentation today[1] is clearly more expansive, better formatted and more readily comprehensible than its documentation from 2001[2] (or even 2008[3]). There are multiple entire sections of documentation now that didn't exist before. Standards were just lower back then, partly because a larger percentage of "programmers" were accustomed to wrestling with beasts like C, and partly because systems were much smaller.

                                                                        https://docs.python.org/3/

                                                                        https://docs.python.org/2.0/

                                                                        https://docs.python.org/2.5/

                                                                        • simonw a day ago

                                                                          https://docs.python.org/3/reference/grammar.html and https://docs.python.org/3/reference/index.html look pretty comprehensive to me, and they're backed up by a thorough collection of PEPs: https://peps.python.org/pep-0000/

                                                                          As a relatively recent example, here's the language reference documentation for the match statement https://docs.python.org/3/reference/compound_stmts.html#the-...

                                                                          • LPisGood a day ago

                                                                            I don’t understand what you mean by “doesn’t have a spec”

                                                                            The existence of Pypy and CPython and separate but compatible entities shows that there is

                                                                            • NeutralForest a day ago

                                                                              That seems uncalled for, the docs are great and the various guides are usually a good starting point to understand and then use the stdlib.

                                                                              • troupo a day ago

                                                                                > Twenty five years ago it was the gold standard of documenting software.

                                                                                That was PHP. Though Python was a close second

                                                                            • satwik a day ago

                                                                              Hi, author of the repo (https://github.com/satwikkansal/wtfpython) here, pleasantly surprised to wake up to this thread about it. The intention of the project is not to put Python in a bad light relative to other languages, the intention was to explore it more deeply through (sometimes contrived) snippets that give non-obvious outputs. I believe for a regular Python programmer, the "Slippery Slopes" section is a must-know. The first section (Strain your brain), does contain some patterns that you may not ever see in real world, the goal there is to just learn about internals. Agreed some of them are very obvious for a well-learned programmer, and some of them aren't even Python specific, it does help a lot of beginner to intermediate programmers. I remember it being given as a supplementary material in one of the CS-based course.

                                                                              It is curated with something-for-everyone approach, I expect people to learn a thing or two about Python / CS after going through the snippets. I haven't updated it in a while in a major way, but looking at the feedback I'll try to organise them better, and add new snippets (I have a long personal list that I maintain) in a future release!

                                                                              • procaryote a day ago

                                                                                I'm happy to complain about python but I got like a third into it and didn't find any actual wtfs.

                                                                                Java as contrast has interning wtfs because == between java objects does essentioally what python's `is` does. Python actually made a good choice here and made == do what you'd expect.

                                                                                Is anyone surprised by `(1 > 0) < 1`? There are languages where it will be rejected because of types, but what else would you expect it do than compare a boolean true to 1?

                                                                                Is there anything of value later on?

                                                                                • bb88 a day ago

                                                                                  I think there's a few in there. But most people aren't going to write code that way. It's good to know, and keep in your head, but if you have to think too hard in the future if a piece of code is correct, it's probably better to rewrite it anyway. And weird code like that shows up like a sore thumb in reviews.

                                                                                  • never_inline 20 hours ago

                                                                                    > Java as contrast has interning wtfs because == between java objects does essentioally what python's `is` does.

                                                                                    It's pretty sensible because i wouldn't expect that operator to call a method - since Java does not have operator overloading.

                                                                                  • meander_water 21 hours ago

                                                                                    This is great. Also to be a little meta, this link was posted in 2019 (https://news.ycombinator.com/item?id=21862073) and got the same sort of comments (how js gets flak, and python doesn't)

                                                                                    • ssalazar a day ago

                                                                                      I didnt make it past the long list of id() misuses, but its a rookie mistake to confuse referential equality with value equality, and when doing so youre in for a bad time.

                                                                                      • timonoko 18 hours ago

                                                                                        Small numbers are "eq" in python. It saves space and time, because probably those numbers dont even exist, and the value is derived from the pointer value.

                                                                                          >>> a=3
                                                                                          >>> b=3
                                                                                          >>> a is b
                                                                                          True
                                                                                          >>> a=1e20
                                                                                          >>> b=1e20
                                                                                          >>> a is b
                                                                                          False
                                                                                        
                                                                                        But in Commonlisp all numbers are "eq". Very strange. Is there heap of numbers and once created, they reuse those?

                                                                                          [4]> (setq b 1E20)
                                                                                          1.0E20
                                                                                          [5]> (setq a 1E20)
                                                                                          1.0E20
                                                                                          [6]> (eq a b)
                                                                                          T
                                                                                        • kazinator 9 hours ago

                                                                                          In Common Lisp, in fact, no numbers are required to be eq! Even (eq 0 0) is not required by ANSI CL to return true.

                                                                                          Why your Lisp implementation produces true for (eq a b) is probably because of the following.

                                                                                          Firstly, in Common Lisp there are single and double precision floats. Floating point tokens written with E or e are single. An implementation is allowed to map that to a 32 bit float. If the implementation has 64 bit values, it can easily arrange for single precision floats to be unboxed without any special tricks; they need only half the word. Try evaluating with 1D+20 to see what that does.

                                                                                          But even 64 bit values can be unboxed with a trick called NAN tagging.

                                                                                          I implemented NAN tagging in TXR Lisp (which has only double-precision floats), enabled on 64 bit targets:

                                                                                          64 bit machine:

                                                                                            1> (eq 1E20 1E20)
                                                                                            t
                                                                                            2> (sizeof val)
                                                                                            8
                                                                                           
                                                                                          32 bit machine:

                                                                                            1> (eq 1E20 1E20)
                                                                                            nil
                                                                                            2> (sizeof val)
                                                                                            4
                                                                                          
                                                                                          On 32 bits, floating-point objects are on the heap.

                                                                                          It's pretty important to have unboxed floats for intense numerical work.

                                                                                          • Jtsummers 12 hours ago

                                                                                            > But in Commonlisp all numbers are "eq"

                                                                                            No, that's not true.

                                                                                            >> numbers with the same value need not be eq,

                                                                                            http://clhs.lisp.se/Body/f_eq.htm#eq

                                                                                            It's not guaranteed that eq will return true for two numbers of the same value in Common Lisp. It happens to work much of the time, but it is never guaranteed to work. For a given implementation it could even be true all the time (as it's not disallowed), but for portability it should not be relied upon.

                                                                                          • omoikane a day ago

                                                                                            See also: https://github.com/satwikkansal/wtfpython

                                                                                            The "interactive notebook" link goes to the colab page.

                                                                                            There are many earlier threads associated with the markdown version, for example:

                                                                                            https://news.ycombinator.com/item?id=21862073 (2019-12-23, 185 comments)

                                                                                            https://news.ycombinator.com/item?id=26097732 (2021-02-11, 163 comments)

                                                                                            https://news.ycombinator.com/item?id=31566031 (2022-05-31, 143 comments)

                                                                                            https://news.ycombinator.com/item?id=37281692 (2023-08-27, 82 comments)

                                                                                            • g42gregory a day ago

                                                                                              I may be wrong, but think a lot of it is undefined behavior. Interpreter may choose to recycle objects or it may not. In case of integers, low enough number in different parts of the program, will be represented by the same object. But if the integer is high enough, different objects will be created. That’s my understanding, but I could be wrong.

                                                                                              • rr808 a day ago

                                                                                                While we're at it. Notebooks aren't a real programming environment.

                                                                                                • extraduder_ire a day ago

                                                                                                  Real in what way? I'm not too familiar with them, but they seem about on par with the environment for JavaScript that is html files or "web pages".

                                                                                                  Making a html document so someone can easily run some JavaScript, seems like the closest parallel to making a jupiter notebook so someone can easily run some python.

                                                                                                  • pseufaux a day ago

                                                                                                    Define "real programming environment"

                                                                                                    Seems like an odd way to say, "Notebooks don't fit my workflow/usecase."

                                                                                                    • klysm a day ago

                                                                                                      Huh we must be hallucinating then?

                                                                                                      • jacquesm a day ago

                                                                                                        Agreed. Even so, you'd be surprised to find that some companies run them in production. I never got the idea behind that, maybe someone that engages in such a practice could enlighten me. I have yet to hear a good rationale.

                                                                                                        • undefined a day ago
                                                                                                          [deleted]
                                                                                                          • mr_toad a day ago

                                                                                                            Programming encompasses things other than production environments and software engineering. Notebooks are fine programming environments for prototyping, exploration and training.

                                                                                                          • incognito124 a day ago

                                                                                                            I don't think anyone is disputing that

                                                                                                            • moffkalast a day ago

                                                                                                              Jupyter is like if someone decided they don't like neither REPL nor running complete scripts and did something that makes a mockery of both.

                                                                                                              • rexpop a day ago

                                                                                                                Yeah, this simply doesn't work for me; ie the expressions did not evaluate, and output was not made visible.

                                                                                                                I have never had a successful experience writing or running programs in notebooks like this--although I have not, arguably, tried with high stakes.

                                                                                                                As soon as the stakes rise that far, however, I reach for a whole-ass Unix environment with a shell, a filesystem, and a stateful process model.

                                                                                                                Is this a crutch? Is it too much overhead? It's arguably absurd that I even ask. (Edit: It's actually killing me how ambiguous I'm being, right now. There's no winning.)

                                                                                                                • zahlman a day ago

                                                                                                                  > As soon as the stakes rise that far, however, I reach for a whole-ass Unix environment with a shell, a filesystem, and a stateful process model.

                                                                                                                  I don't understand what you mean by "reach for". Don't, for example, Linux desktop users have all these things at hand all the time? For me it would take much more effort to set up a "notebook" than a virtual environment.

                                                                                                              • globalnode a day ago

                                                                                                                Some ways of programming in Python require a LOT of mental effort. And some don't. For example you can do a lot in one line in Python, but I usually have to write extensive comments and references to external docs to keep track of what that one line is doing. I think sometimes it would be easier if I just had to write 3 or 4 lines of self evident code and use that as docs instead.

                                                                                                                • bb88 a day ago

                                                                                                                  Having written python professionally for more than a decade, I highly recommend not writing complex one-liners -- unless there's some real benefit to doing so.

                                                                                                                  The more one-liners there are in the code base, the more impenetrable and harder to debug the code is. Unrolling nested functions and switching comprehensions to lists and if statements often make it cleaner and easier to write unit tests for.

                                                                                                                  If you rely upon code coverage for unit tests, a complex list comprehension won't help you see if you have hit the branch cases.

                                                                                                                  • throwaway2037 21 hours ago

                                                                                                                    Personally, I always find multi-layered list comprehensions the hardest to read... And debugging list comprehensions is basically impossible in my experience.

                                                                                                                  • never_inline 20 hours ago

                                                                                                                    Use static types and treat python as more concise java. Use dynamic types sparingly. Limit expression complexity.

                                                                                                                    Static types + IDE features make it much easier to understand any codebase.

                                                                                                                  • hislaziness a day ago

                                                                                                                    Great. I enjoy these kind of articles. My all time favorite book for 'C' is Expert C Programming: Deep C Secrets.

                                                                                                                    • zbentley a day ago

                                                                                                                      It’s reassuring that a lot (feels like a majority) of these are noted as resolved or turned into errors that make more sense than confusing behaviors in later editions of the language.

                                                                                                                      Python 3.5, which is what the WTFs use by default, is relatively ancient.

                                                                                                                      • explain2me 12 hours ago

                                                                                                                        Holly cow, what a mess. Is the miniscule efficiency gain really worth it especially in such a user friendly language as Python?

                                                                                                                        • pyuser583 a day ago

                                                                                                                          Exclamation points are in fact part of the ASCII standard.

                                                                                                                          • JohnKemeny a day ago

                                                                                                                            If you use a floating point number as a key in a `dict` the wtf should not be pointing at Python.

                                                                                                                            These are not wtfs, please.

                                                                                                                            These examples read like someone who is learning to program and who is confused.

                                                                                                                                x is not y
                                                                                                                                x is (not y)
                                                                                                                            
                                                                                                                            How can you confuse these two?!
                                                                                                                            • woooooo a day ago

                                                                                                                              Fp32 or fp64 in a strongly typed language would be no problem as hash keys. Yes, they're imprecise for math but the 64 bits of data should work just like a long as a hash key.

                                                                                                                              • marcosdumay a day ago

                                                                                                                                No, they are not suitable for use as hash keys unless you are treating them as opaque binary data. And if you are there, you are better putting your values in some type that means opaque binary data for the start.

                                                                                                                              • mvanbaak a day ago

                                                                                                                                AI

                                                                                                                                • undefined a day ago
                                                                                                                                  [deleted]
                                                                                                                              • happytoexplain a day ago

                                                                                                                                I'm a little flabbergasted at how many comments here boil down to "These aren't WTFs if you understand the language."

                                                                                                                                • pseufaux a day ago

                                                                                                                                  It's a bit like when non-JS programmers complain about JavaScript. It's mostly nonsense. That's not to say that JavaScript doesn't have flaws and footguns, but the opinion of novices in the language is not who I am interested in learning from. Python is the same. It has flaws and idiosyncrasies, especially in the standard library (cough cough xml.ElementTree), but the core language is fairly consistent and predictable. Expecting casting functions like bool() to parse strings like JavaScript does makes little sense.

                                                                                                                                • undefined a day ago
                                                                                                                                  [deleted]
                                                                                                                                  • evanjrowley a day ago

                                                                                                                                    Now do a "What the Fuck Excel"

                                                                                                                                    • throwaway2037 21 hours ago

                                                                                                                                      Good point. The "snap to zero" rules are wild in Excel.

                                                                                                                                    • ltbarcly3 a day ago

                                                                                                                                      Haha these aren't even wtfs! the id changes or not? oh noooo, people use the id() function so much, I've literally seen it 0 times ever in production code. This is nothing like the JS wtfs where there is no way to predict what it will do, and it violates it's own patterns.

                                                                                                                                      edit: to be more clear, I have run into a lot of JS wtf's trying to write code for work. you just run into bugs all the time where JS doesn't do anything like what would be the obvious thing to do, or the thing that works 99.99% of the time fails depending on the value of the string being 'magic'. with Python I rarely if ever learn some bizarre arcane thing accidentally because it almost always behaves as expected. If you think there's an equivalence here you are just a JS fanboy, JS was a language that evolved largely because of hacks vendors put in and those insane hacks got standardized because it was too late, despite being insane. Python was specifically and carefully designed to minimize unexpected behavior.

                                                                                                                                      • craftkiller a day ago

                                                                                                                                        I think this one is good:

                                                                                                                                          False == False in [False]
                                                                                                                                          # Outputs True
                                                                                                                                        
                                                                                                                                        It doesn't make sense no matter which way you evaluate it. Equality first:

                                                                                                                                          False == False # True
                                                                                                                                          True in [False] # False
                                                                                                                                        
                                                                                                                                        or `in` first:

                                                                                                                                          False in [False] # True
                                                                                                                                          False == True # False
                                                                                                                                        • zahlman a day ago

                                                                                                                                          > It doesn't make sense no matter which way you evaluate it

                                                                                                                                          It makes sense when you evaluate it the way it's intended to be understood (https://stackoverflow.com/questions/6074018). There are other languages that do this, and it was consciously designed to work this way. The idea that you should have to evaluate one of the operators "first" may make sense to someone trying to implement a parser with techniques from the 70s, but for languages like Python, the design is clearly more about what makes sense for the code author.

                                                                                                                                          Arguably it's surprising that `in` is treated as on par with `==`. But on the other hand, even someone trying to implement a parser with techniques from the 70s would have to admit that both are "relational operators".

                                                                                                                                          • None4U a day ago

                                                                                                                                            Yeah, having the expansion rule operate on operators that are clearly not transitive serves no purpose (1 < 2 < 3 is at least a math notation)

                                                                                                                                            • daedrdev a day ago

                                                                                                                                              Its apparently trying to allow x < y < z be a legal statement, meaning that we get:

                                                                                                                                              False == False and False in [False] is a expansion of that statement, just like x < y and y < z is the expansion of x < y < z

                                                                                                                                              • jnpnj a day ago

                                                                                                                                                Oh yeah, that's a great one, there must be a compounded boolean expression logic escaping us

                                                                                                                                                I'll add:

                                                                                                                                                    >>> False == False in [True]
                                                                                                                                                    False
                                                                                                                                                    >>> True == True in [True]
                                                                                                                                                    True
                                                                                                                                                
                                                                                                                                                ps: some ast poking

                                                                                                                                                    >>> ast.show(ast.parse("False == False in [False]"))
                                                                                                                                                    Module(
                                                                                                                                                      body=[
                                                                                                                                                 Expr(
                                                                                                                                                   value=Compare(
                                                                                                                                                     left=Constant(value=False),
                                                                                                                                                     ops=[
                                                                                                                                                       Eq(),
                                                                                                                                                       In()],
                                                                                                                                                     comparators=[
                                                                                                                                                       Constant(value=False),
                                                                                                                                                       List(
                                                                                                                                                  elts=[
                                                                                                                                                    Constant(value=False)],
                                                                                                                                                  ctx=Load())]))])
                                                                                                                                                
                                                                                                                                                    >>> ast.show(ast.parse("(False == False) in [False]"))
                                                                                                                                                    Module(
                                                                                                                                                      body=[
                                                                                                                                                 Expr(
                                                                                                                                                   value=Compare(
                                                                                                                                                     left=Compare(
                                                                                                                                                       left=Constant(value=False),
                                                                                                                                                       ops=[
                                                                                                                                                  Eq()],
                                                                                                                                                       comparators=[
                                                                                                                                                  Constant(value=False)]),
                                                                                                                                                     ops=[
                                                                                                                                                       In()],
                                                                                                                                                     comparators=[
                                                                                                                                                       List(
                                                                                                                                                  elts=[
                                                                                                                                                    Constant(value=False)],
                                                                                                                                                  ctx=Load())]))])
                                                                                                                                                
                                                                                                                                                note the first tree as one Comparator node with multiple ops, time to read the spec to know how these are evaluated
                                                                                                                                                • its-summertime a day ago

                                                                                                                                                      return False == False in [False]
                                                                                                                                                  
                                                                                                                                                  is roughly

                                                                                                                                                      a = False
                                                                                                                                                      b = False
                                                                                                                                                      c = [False]
                                                                                                                                                      (a == b) and (b in c)
                                                                                                                                                  
                                                                                                                                                  
                                                                                                                                                  is less roughly

                                                                                                                                                      a = False
                                                                                                                                                      b = False
                                                                                                                                                      if not (a == b):
                                                                                                                                                         # short circuits https://news.ycombinator.com/item?id=44619401
                                                                                                                                                         return False 
                                                                                                                                                  
                                                                                                                                                      c = [False]
                                                                                                                                                      if not (b in c):
                                                                                                                                                         return False
                                                                                                                                                  
                                                                                                                                                      return True
                                                                                                                                                  • ltbarcly3 a day ago

                                                                                                                                                    Ok I'll admit this is a real WTF

                                                                                                                                                  • kgm a day ago

                                                                                                                                                    I have used id() in production code, but the circumstances were extremely specific: It was in some monitoring-related code that was trying to dig down into the guts of the interpreter for the purposes of tracking memory usage on the level of individual pages. It was a whole thing.

                                                                                                                                                    Which is to say: Users of Python almost certainly don't want to call id(), unless they are mucking around with something related to the functioning of the interpreter itself. The only other circumstance in which I've used it in anything like real code was in a quick hack where what I really wanted was to put a bunch of dicts in a set, but you can't do that, so I made a set of their IDs instead (but this wasn't in what I would call "production code").

                                                                                                                                                    In general, most of the "wtfs" listed here are pretty banal. If you're using `is` on immutable objects, you're basically just asking for trouble. The functioning of string and integer interning and constant folding are implementation curiosities, and if you ever write code in which such differences matter, then you have made an error.

                                                                                                                                                    • jtolmar a day ago

                                                                                                                                                      I don't think either reach the real wtfs of their language. id() does some weird things with string interring, javascript coerces cross-type arithmetic into weird nonsense instead of throwing a type error. Whatever, neither of these come up in the real world.

                                                                                                                                                      Builds so messy that you need to resort to venv, every second DOM API being someFunc(thirdMostImportantArgument, { mostImportantArgument: val }), these are the real wtfs.

                                                                                                                                                      • zahlman a day ago

                                                                                                                                                        Yes, but those are definitionally library and ecosystem wtfs, rather than language ones.

                                                                                                                                                      • mvanbaak a day ago

                                                                                                                                                        I totally agree with you. I have seen a lot of code, but the id() function has never been used in production code

                                                                                                                                                        • Shacklz a day ago

                                                                                                                                                          > I have run into a lot of JS wtf's trying to write code for work

                                                                                                                                                          JS certainly does have plenty of wtfs but honestly, when sticking to somewhat modern JS, it's not so bad anymore these days. Function-scoping-shenanigans can mostly be circumvented by simply sticking to const/let, use a 3rd-party-library like date-fns instead of the dumpsterfire that is native Date and you're almost there - throw typescript into the mix and I have a very hard time imagining running into the meme-worthy JS-warts on a daily basis.

                                                                                                                                                          • andy_ppp a day ago

                                                                                                                                                            I think they are pretty bad tbh apart from the ID one. Everyone knows about the JS quirks so I think it’s good to know sometimes equality in Python strings can behave extremely strangely and the other issues mentioned. Why does it have to be defended “my language is better than your language”? However I’d like to say Elixir is near perfect, thanks :-)

                                                                                                                                                            • ltbarcly3 a day ago

                                                                                                                                                              No you misread it, equality for python strings works exactly the way you expect. the id() value (which is basically the memory address) might not change if you concatenate strings, but literally nobody ever uses id for anything ever. You would use hash() which does change appropriately.

                                                                                                                                                              edit: 'wtf!' is always equal to itself. Maybe you are thinking of `is`, which just compares the value of id(). `is` can't be used for comparisons, saying it's a WTF is just being unfamiliar with basics. `is` is not used for equality comparisons. This is how more or less every language works, comparing pointers is not the same as comparing strings in C/rust/etc. Comparing object identity is not the same as testing equality in Java.

                                                                                                                                                            • zahlman a day ago

                                                                                                                                                              Like I've been saying in other recent submissions, since there seems to be a trend for similar discussion: in Python you find WTFs by trying to probe how Python works, pushing deliberately over-flexibly customization hooks to their limits, etc. In JS you find them by merely trying to call builtins with dodgy inputs.

                                                                                                                                                              • w4yai a day ago

                                                                                                                                                                And now you're using pretty much the same rhetoric than JS guys when facing to wtfjs.

                                                                                                                                                                • ltbarcly3 a day ago

                                                                                                                                                                  No, I don't think so. The JS wtf's actually make you gasp. These are so obscure and boring.

                                                                                                                                                                  F'ing JS equality isn't transitive for builtin types.

                                                                                                                                                                  • BobbyTables2 a day ago

                                                                                                                                                                    Even normal JS is WTF-worthy…

                                                                                                                                                                    Allowing out of bound array indexing and operations using “undefined” seems pretty crazy to me.

                                                                                                                                                                  • undefined a day ago
                                                                                                                                                                    [deleted]
                                                                                                                                                                  • ValveFan6969 a day ago

                                                                                                                                                                    [dead]