3 ms·
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 ca
by Danjoe4 1y 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.
- deleted 1y ago[deleted]
- motorest 1y 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?
- fragmede 1y agoNot 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 1y agoSo we shouldn't learn about them? They're offensive to enumerate? No, they're useful to understand.
- fragmede 1y agoSo we should put them into the bug tracking system and prioritize them accordingly. They're very useful to understand, but if one costs $100 every time it happens but it only happens once a year, vs a different bug that costs $1 every time it gets hit, but it happens every day, which one should get fixed first?
- motorest 1y agoI 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?
- fragmede 1y agoI think you're reading a lot more emotion than exists into my comment. Despair? lol I'm just pointing out that QA testers (and good ones are worth their weight in gold) find bugs by doing things must users wouldn't even consider doing. https://news.ycombinator.com/item?id=25851770 https://news.ycombinator.com/item?id=25851770
- FreakLegion 1y agoWhich 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.
- duncanfwalker 1y agoI 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
- evertedsphere 1y 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
- crazygringo 1y 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.
- happytoexplain 1y agoWhy 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.
- coldtea 1y agoYou 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.
- sundarurfriend 1y 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.
- constantcrying 1y 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.
- pyuser583 1y ago“Is” checks memory location of the values.