Test 0 and false since false is 0

Peter Otten __peter__ at web.de
Fri Jul 7 11:17:48 EDT 2017


Nathan Ernst wrote:

> On Fri, Jul 7, 2017 at 2:04 AM, Peter Otten <__peter__ at web.de> wrote:

>> >>> sorted([0.0, 0, False, [], "x"], key=lambda x: x == 0 and type(x) ==
>> int)
>> [0.0, False, [], 'x', 0]

> You'd be better off using the builtin "isinstance" function, e.g.:
> isinstance(x, int). This also has the added benefit of working nicely with
> inheritance (isinstance returns true if the actual type is derived from
> the classinfo passed as the second argument). See
> https://docs.python.org/3/library/functions.html#isinstance for details.
 
Hm, I suggest that you run my code sample above with your suggested 
improvement ;)




More information about the Python-list mailing list