On Sun, 26 Feb 2012 12:56:46 +0100, Wolfgang Meiners wrote:
> That means:
> if maxlength and (len(string) <= maxlength):
>
> is equivalent to
> if (maxlength is not None) and (len(string) <= maxlength):
>
> which is more complicated to type and -in my opinion- not so intuitive.
> But because it is equivalent, it is a matter of taste, what to use.
Incorrect. The two are *not* equivalent.
def test(maxlength, string):
flag1 = maxlength and (len(string) <= maxlength)
flag2 = (maxlength is not None) and (len(string) <= maxlength)
return bool(flag1), bool(flag2) # normalise to booleans
>>> test(0, '')
(False, True)
So the two forms will take opposite branches of the if statement when
maxlength is 0 and string is the empty string.
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list