checking if a list is empty
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed May 11 21:07:05 EDT 2011
On Wed, 11 May 2011 17:38:58 -0500, harrismh777 wrote:
> Steven D'Aprano wrote:
>>> modelled. Lists do not have truth values in the application domain
>> Yes they do. Empty lists are nothing, ergo false, and non-empty lists
>> are something, ergo true.
>>
>>
> No they don't. Empty lists are empty lists... which just happen to
> become False when type cast bool(list[])--> False.
>
> lists cannot have a truth... binary False or True... however, when cast
> with bool(list) they can be used in a test (even with 'not') to make
> some decision.
Have you been not paying any attention at all?
>>> alist = []
>>> if alist: pass
... else: print "alist is a false value"
...
alist is a false value
>>>
>>> if type(alist) is bool: pass
... else: print "but not a bool"
...
but not a bool
> Lists by themselves, empty or not, cannot have a 'truth' in an of
> themselves.
Despite your confident tone, they really do. Just like EVERY OTHER OBJECT
IN PYTHON. And Perl, and Ruby, and Javascript, and PHP, and many other
languages. You might not like that fact, but it is a fact.
--
Steven
More information about the Python-list
mailing list