4 ms·
The Pythonic Emptiness
- 082349872349872 2y ago[flagged]
- deleted 2y ago[deleted]
- JackSlateur 2y agotldr: do not use 'if len(list) == 0', use 'if not list' ! You just have to not write any bug in your code. Also, use type checking everywhere. And rewrite your mind, too. The benefits are worth it .. ! Oh well;
- bogwog 2y ago> Also, use type checking everywhere I can't think of anything more Pythonic than that!
- ReflectedImage 2y agoUse the simplest syntax and check the code works via unit testing like you should be doing. Don't statically type your code as it increases the bugs by a factor of 3x typically.
- wiml 2y agoHow does statically typing your code "increase[] your bugs by a factor of 3x" when it has no runtime effect? What orifice are you getting the 3x from?
- ReflectedImage 2y agoTypically, software developers write 3x the number of lines of code when using static typing compared to duck typing. It's just the nature of the static typing code style. Write 3x the lines, get 3x the bugs. The programming language that has the least measured bug in practice is Clojure because it is duck typed and because it doesn't use OOP. Both static typing and OOP have a significant measurable negative effect on code correctness.
- stnmtn 2y agoClojure has the least measured bugs in practice? Where does that statistic come from?
- xen0 2y agoI too can invent numbers. My invented numbers say there's no meaningful difference in line counts between statically typed and dynamically typed languages. So how will we prove any of us wrong?
- deleted 2y ago[deleted]
- sjsdaiuasgdia 2y agoI'm mostly annoyed that 'if len(items) == 0' / 'if len(items) > 0' aren't presented as options. If we're talking about readability, they're far clearer than either of the options in the article and require no pre-knowledge of truthiness rules.
- warkdarrior 2y agoThey are presented as options, it's just that their performance sucks. From the article: if not mylist: # 1.061 if len(mylist) == 0: # 1.924
- James_K 2y agoI feel like, if you care about performance, using Python at all is a mistake.
- tantalor 2y agoFor most applications, the choice of algorithms and data structures has more impact on performance than programming language.
- LegionMammal978 2y agoThe one big exception to this I've found is working with image data. When you have over a million pixels, you'll have a slow time transforming them unless you can process each one within nanoseconds. Even a compiled language like Rust will struggle with image data if you build it in debug mode. So there's really always going to be a place for optimized implementations of those sorts of things.
- chikere232 2y agoThen caring about a microptimisation over readability would still be the wrong call
- bogwog 2y agoWhen you profile your code and find that all you gotta do is change a few if statements for a 2x perf boost, that will be a happy day.
- James_K 2y agoIsn't this just a Perl feature (arrays are also their length and zero values are falsy)? I can't help but feel Python is getting closer to Perl as time goes on. Ironic, since their original goal was to be simple and make themselves distinct from Perl. What was the saying again? "There should be one-- and preferably only one --obvious way to do it." Honestly, I think it was all this "Zen" stuff that lead Python down the path to weirdness. This article reads like a monk interpreting sacred text. I can think of no good reason for all this malarkey.
- jerf 2y agoThis can't be Python "getting closer" to Perl, since it's been this way in Python since inception, and I'm pretty sure, Perl as well. Both languages have always been exactly where they are now on the topic, with no motion, probably since the beginning, and certainly for multiple decades.
- James_K 2y agoI'm not particularly well versed in the history of either language. I just know that Python was supposed to be "simple and intuitive", but I've had quite a different experience of it and this has often been down to Perl-like things going on.
- jerf 2y agoMy personal opinion is that Python gave up on "simple and intuitive" a long time ago and people still citing that as a guiding principle of the language really need to sit down and just read the Python documentation again from start to finish, and then ponder for a moment. If they still need a clue, they are invited to the same thing to, say, Python 2.1, and ponder for a few more moments. It clearly isn't and hasn't been for a while. That is not, on its own, a bad thing necessarily. It just means it isn't a guiding principle anymore, or at the very least, it has moved way down the priority list. Plenty of languages have changed their guiding principles over time. However, this is not an example of that. "if list:" has been the idiomatic Python since inception, and "if len(list):" has been an unnecessary complication for the same period of time. Python's "preferably one way to do it" has never been about "there is literally only one syntactically valid way to do it", for fairly obvious reasons if you think about it.
- int_19h 2y agoI don't find any of the arguments here particularly convincing. This claim in particular is weird: > Similarly, when you use len() to check a sequence for emptiness, you are reaching out for a more powerful tool than necessary. As a result it may make the reader question the intent behind it, e.g. if there is a possibility of handling objects other than a built-in sequence type here? Given that checking for truthiness is less strict than a length test, by the same token, whenever you use it, you're reaching for an even more powerful tool than necessary. And, if anything, seeing `not items` is what makes me question the intent - did the author mean to also check for None etc here, or are they just assuming that it's never going to be that? And sure, well-written code will have other checks and asserts about it - but when I'm reading your code, I don't know if it's missing an assert like that because you intended it, or because you couldn't be bothered to write it. OTOH len() is very explicit about what is actually checked, and will fail fast and loudly if the argument is not actually a sequence. Also note that it's not, strictly speaking, an either-or - you can use `not len(x)` instead of `len(x) == 0` if you want a distinctive pattern specifically for empty collection checks.
- adamc 2y agoI don't agree with this at all, and I wonder if it reflects what other languages you use that may have shaped your assumptions. `if mylist` feels very much like Common Lisp to me. In much of the code I've seen, the value would never be none because an empty list was definitely created.
- tromp 2y agoI would prefer a standard list method/function to test for emptiness, which would be both readable and efficient.
- chikere232 2y agoIt won't be efficient as it's python, but it could be readable
- mulmboy 2y ago`if x: foo()` is a cancer on the Python community. Devs often use it with the intention of handling x being None, and carelessly lump in zero and empty lists/strings at the same time. Endless bugs.
- smetj 2y agoYup!
- morkalork 2y agoHoly moly, that meme about type checkers and variable names, someone is arguing for hungarian notation in 2024?!
- pseudalopex 2y agoSomeone argued user_list, user_count, and has_users are clearer than users, users, and users. Will you argue the opposite? The original Hungarian notation was a less readable implementation of the same idea. The Hungarian notation most people hated replaced functional types like count and index with data types like unsigned long. And used them everywhere.
- lihaoyi 2y agoPython's truthiness behavior was the trigger for one of my worst ever bugs early in my career, which not only pulled in senior engineering but also marketing/comms and legal to help sort out the mess. Not a fan!
- adamc 2y agoI think this needs more explanation to know if this is a good argument or not.
- FreakLegion 2y agoKeep in mind that truthiness comes from __bool__ and is overridable, so separate from Python itself, a lot of library authors have made questionable decisions here. A perennial contender is https://github.com/psf/requests/issues/2002 https://github.com/psf/requests/issues/2002.
- aguaviva 2y agoYou know you're going to need to provide us with a little snippet demonstrating this behavior now, right?
- lihaoyi 2y agoI went and dug up the original code that caused an issue. Here it is: https://github.com/python/cpython/blob/v2.7.1/Lib/cgi.py#L602-L603 https://github.com/python/cpython/blob/v2.7.1/Lib/cgi.py#L60... Python's std lib `cgi.FieldStorage` object was falsy if it did not define any headers, even if it contained file data. Thus my conditional trying to check whether a file was being uploaded "if request.field_storage" was going through the False branch when files were being uploaded but only in certain header-free scenarios not covered by automated and manual testing. This resulted in us dropping user data on the floor and losing uploaded files for a very large number of users before we realized and shut it off The other sibling post contains another example where people may be confused, and Google pulls up others. But this is the concrete case that caused us to send out a hundred thousand apology emails to affected customers after losing their files
- FridgeSeal 2y agoPython’s “truthiness” is a cutesy feature that is just an excuse for bugs in your code. It’s opaque/ too magic, exhibits poor readability and endless confusion. Just use a normal check, like everyone is expecting to see. “Oh but what if it’s not a sequence”, well then you have bigger problems. Why are you emptiness testing something that may-or-may-not-be-a-sequence? Maybe solve that problem first.
- Chris_Newton 2y agoPython’s “truthiness” is a cutesy feature that is just an excuse for bugs in your code. It’s opaque/ too magic, exhibits poor readability and endless confusion. Indeed. Relying on truthiness has always felt very un-Pythonic to me, not least because it contradicts several principles in the Zen of Python: • Explicit is better than implicit. • Special cases aren’t special enough to break the rules. • There should be one — and preferably only one — obvious way to do it.
- wodenokoto 2y agoThe idea that `if len(items):` indicates that you expect other things than sequences seems backwards to me. It’s the duck typing nature of, classic Python, that leads the community to recommend the broader `if items:`, which allows for numbers and such.