osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Test 0 and false since false is 0


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 ;)