Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Jan 23 06:55:31 EST 2008
On Wed, 23 Jan 2008 09:30:28 +0000, Duncan Booth wrote:
> Kristian Domke <news at neither-nor.net> wrote:
>
>> foo = (not f and 1) or 0
>>
>> In this case f may be None or a string.
>>
>> If I am not wrong here, one could simply write
>>
>> foo = not f
>>
>>
> Yes, it sounds pretty silly, and not just on the level you spotted.
>
> The only difference between the two expressions is that the original
> sets foo to an integer whereas your version sets it to a bool. So the
> question of which is most appropriate actually comes down to what foo is
> being used for.
But since Python bools are subclasses from int, both of them are actually
ints. One happens to look like 1, and the other looks like True.
--
Steven
More information about the Python-list
mailing list