4 ms·
Python has truthiness and falsiness as well. That shortens code like: if not x: do_something() x could be 0, None, False, empty set set(), {}, [],
by rdtsc 13y ago
Python has truthiness and falsiness as well.
That shortens code like:
if not x:
do_something()
x could be 0, None, False, empty set set(), {}, [], (), '',"" or an I believe any class or object that appropriately overrides __nonzero__() or __bool__() methods.
It is a matter of taste, so I rather like it.
- nbouscal 13y agoIt's not a matter of taste when it breaks functionality.
- rdtsc 13y agoIt depends on what x is and what do_something() does. Most often than not in my code that wouldn't break functionality. In the case of dates it is broken, so same code pattern there would be broken. At this point a lot more functionality would break if 0 become non-False or threw a TypeError exception.
- famousactress 13y agoI appreciate falsiness in python as well (especially for None, [], {}, set([]))... Interestingly I think zero is arguably the most dubious of the falsies (not that I'd recommend changing it, for obvious backward compatibility pain), but I can remember being burned by bugs from zero evaluating False a number of times, yet I can't once remember being burned by any of the other values I listed burning me. I do hope the ticket in question is reconsidered though. Midnight is obviously a "valid and populous instance" that shouldn't evaluate False.
- mercurial 13y ago> I appreciate falsiness in python as well (especially for None, [], {}, set([]))... Interestingly I think zero is arguably the most dubious of the falsies (not that I'd recommend changing it, for obvious backward compatibility pain), but I can remember being burned by bugs from zero evaluating False a number of times, yet I can't once remember being burned by any of the other values I listed burning me. Agreed, "0 as False" sounds like "let's take a leaf from C and its bad type system". I generally rely on falsiness as well, except when dealing with number, and the inconsistency is annoying. I also agree that midnight is not inherently more false than noon or any other date. But I guess that's a risk with falsiness, just like it is with other magic methods: sometimes you take the magic too far.
- maaku 13y ago"0 is False" is extremely useful to a Pythonista. Ever indexed into an array with a boolean offset? Yes, you can do that. (False is 0, True is 1). Makes for very succinct code.
- pizza 13y ago>>> bool() == int() True
- deleted 13y ago[deleted]
- anon1385 13y agoWell, if you read the mailing list thread there are several core python maintainers saying that is bad style. So I wouldn't call it just a matter of taste when core maintainers disagree with it and parts of the standard library break your code when you do it.
- rdtsc 13y ago> re several core python maintainers saying that is bad style. Well I am a python programmer that has been using it for almost 10 years, 7 full time, and I say it is nice. So yes, I would call it as a matter of taste.