and [True,True] --> [True, True]?????
Gerhard Häring
gh at ghaering.de
Mon Apr 20 18:32:44 EDT 2009
Martin v. Löwis wrote:
>> Are they widespread? I haven't noticed, yet.
>>
>> I prefer to write it explicitly:
>>
>> if len(lst) > 0:
>
> I prefer to test explicitly for the truth value of the
> list. I don't want to test whether the length of the list
> is greater than 0 (in fact, I don't care about the length
> property of the list at all) - I want to know whether the
> list is empty (or not empty). The Python syntax for this
> test is
>
> if lst:
> # not empty
>
> or
>
> if not list:
> #empty
> [...]
You're right - as most of the time ;-) This makes a lot of sense to me.
The reason I preferred len(), btw., was only that len() make it clear
that the argument is a sequence.
Maybe I was just too annoyed by lots of Python code I read that looked
like this:
def foo(x, y, z):
if x:
...
else:
...
with poorly named variables where I didn't know what the heck the
variables are (bool, list, instance, ...). I hate it when I have to look
for the actual method calls to figure out what's going in. Better
variable naming and small comments would often help.
-- Gerhard
More information about the Python-list
mailing list