Python 不同对象比较大小示例探讨

yipeiwu_com6年前Python基础

万恶的源泉:

Fireboo的疑问(当然 lambda 本身写的就有问题):

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

后来几经周折,和 Fireboo 讨论了下,是

1.不同对象比较(除了 number 之外),是按照 type names 比较,

2.当相同类型对象不支持适当比较的时候,采用 address 比较

3.list 与 list, tuple 与 tuple 采用字典序比较

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

相关文章

Python线程条件变量Condition原理解析

这篇文章主要介绍了Python线程条件变量Condition原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Condition...

如何通过Python实现标签云算法

如何通过Python实现标签云算法

标签云(Tag Cloud)常见于各种博客站点中,标签有利于网站内容分类,还可以用于相关性内容推荐。近日笔者有空把个人的开源博客Django_blog添加了一个新功能--标签云。 实现...

python 捕获 shell/bash 脚本的输出结果实例

#!/usr/bin/python ## get subprocess module import subprocess   ## call date command ##...

基于python中staticmethod和classmethod的区别(详解)

例子 class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classm...

使用python开发vim插件及心得分享

使用python开发vim插件及心得分享

vim有各种强大的插件,这不仅归功于其提供的用来编写插件的脚本语言vimL,还得益于它良好的接口实现,从而支持python等语言编写插件。当vim编译时带有+python特性时就能使用p...