Post AtLFfc98Ie0pJyIQvw by ax6761@freeradical.zone
(DIR) More posts by ax6761@freeradical.zone
(DIR) Post #AtD1dPG4IG109Buivo by stfn@fosstodon.org
2025-04-18T07:57:53Z
0 likes, 0 repeats
OK, that surprised me, I did not expect that outcome. I assumed all() for an empty list will return False? And it explains why my code was not doing what I wanted xD#python
(DIR) Post #AtD1gYh5fSUoHT3Yci by stfn@fosstodon.org
2025-04-18T07:58:27Z
0 likes, 0 repeats
Five years working professionally as a Python developer and that language still surprises me in the most basic things
(DIR) Post #AtD2BL6EGIIx5xUZhg by chrysn@chaos.social
2025-04-18T08:03:58Z
0 likes, 0 repeats
@stfn This is not specific to Python: it's the same in Rust and general mathematics. all(empty) is true and any(empty) is false.This aligns well both with implementations (a la `for item in iterator: if item: return false` and eventually return true) and with the definition (there is not a single element in there that is false, so the result must be true).
(DIR) Post #AtD2aY82xLSwkDMb2G by stfn@fosstodon.org
2025-04-18T08:08:34Z
0 likes, 0 repeats
@chrysn Thank you for explaining that, this is where my zero training in maths shows
(DIR) Post #AtE1G76ho4qyLnO652 by ygor@fosstodon.org
2025-04-18T19:28:20Z
0 likes, 0 repeats
@stfn It's one of those things that seem counterintuitive at first, but actually makes perfect sense when you think about it. It's similar to how 5⁰ is 1 and not 0.https://en.wikipedia.org/wiki/Identity_element
(DIR) Post #AtE1v1VXWvZ6rCVjsG by abosio@fosstodon.org
2025-04-18T19:35:46Z
0 likes, 0 repeats
@stfn 17 years, same
(DIR) Post #AtLCgPHxI34Vs36pwe by ax6761@freeradical.zone
2025-04-22T06:39:18Z
0 likes, 0 repeats
@stfn A working, possibly saner, solution ...def check_the_list( the_list ): if not isinstance( the_list, list ): raise ValueError('Need a list, not whatever else was sent.') if len( the_list ) < 1: raise ValueError('Need a non-empty list.') return#Python FTW (has same letters as "WTF")
(DIR) Post #AtLFPrF2PNDfVMXqzo by stfn@fosstodon.org
2025-04-22T07:10:00Z
0 likes, 0 repeats
@ax6761 doing isinstance() is an antipattern in Python, the more correct way is to try to iterate over the variable and check if it errors out.if len( the_list ) < 1:you can just do "if the_list:" instead of that
(DIR) Post #AtLFfc98Ie0pJyIQvw by ax6761@freeradical.zone
2025-04-22T07:12:49Z
0 likes, 0 repeats
@stfn On first point, suit yourself.On second, will not do so anymore for getting burnt earlier. See https://anticapitalist.party/@thezerobit/114339028250062788 & https://freeradical.zone/@ax6761/114340970405388676 .
(DIR) Post #AtLGd1qkNWH27m1lk8 by stfn@fosstodon.org
2025-04-22T07:23:35Z
0 likes, 0 repeats
@ax6761 well, that's why your write a lot of unit tests and use a type checker like mypy when developing the code. If you need to code defensively and check for types during program execution, then it is suggesting something is wrong with your architecture