Quote:
Originally Posted by Starson17
Are there any cases where a datatype comments field can have a len(comments) == 0 and comments != False?
I have a test in the non-custom comments field of:
Code:
if not dest_mi.comments or len(dest_mi.comments) == 0:
I can't recall if I did this, not knowing any better, or if Kovid added it because I didn't. In Python, wouldn't the comments field always be False if its length was zero?
|
The python manual says:
Quote:
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
|
Thus the first test will return True if the string is None or empty, and therefore obviates the need for the second.
I often use the test 'if str: to test if the string has an 'interesting' value.