Python sample discussion of comparing sizes of different objects
- 2020-04-02 13:58:33
- OfStack
The source of all evil:
Fireboo's question (although lambda itself has problems) :
>>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, 3 ] )
[[1, 2, 3], 3]
? :
>>> 1 < [ 1 ]
True
>>> int < list
True
>>> dict < int < list
True
>>> int < map
False
And then after a little bit of discussion with Fireboo, yeah
1. Different objects are compared (except number) by type names,
2. Address comparison is used when appropriate comparisons are not supported for objects of the same type
3. List and list, tuple and tuple are compared in lexicographical order
>>> x = 1
>>> y = [ 1 ]
>>> type( x )
<type 'int'>
>>> type( y )
<type 'list'>
>>> x < y
True
>>> type( int )
<type 'type'>
>>> type( list )
<type 'type'>
>>> id( int )
505552912
>>> id( list )
505555336
>>> int < list
True
>>> type( map )
<type 'builtin_function_or_method'>
>>> type( list )
<type 'type'>
>>> map < list
True