Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?

Duncan Booth duncan.booth at invalid.invalid
Wed Jan 23 13:31:18 EST 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:

> 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. 

The twisted code isn't incorrect, just twisted. :)



More information about the Python-list mailing list