Python tricks(5) -- string和integer的comparison操做

咱们都知道, python是一个强类型的语言, 也是一个动态类型的语言. 可是在python2.X系列中, 这个强类型是须要打折扣的, 是很是接近强类型.html

咱们来看下面的代码片断python

In [1]: 'a' < 1000
Out[1]: False

字符串和整型竟然能够比较, 这个是个很是奇怪的行为. 强类型的语言是不该该容许有这种类型间的隐式转换的, 因此这种比较应该是报错的才对. Java就是这样的一个语言. 不过强类型的语言中, 是能够各类数字类型之间存在隐式转换的.编程

python的这个字符串和整形的比较是很是特别的, python中是没有字符类型(char)的, 单引号和双引号都是字符串类型. 测试

这字符串和整型的比较是按照ASCII表的顺序么? 显然也不是. 'a'对应的ascii码是97, 'a' < 1000就是97 < 1000,  应该返回True才对.spa

官网文档(参考3)对这个东西做出这样的解释:htm

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.对象

CPython的实现细节汇总: blog

规则1: 除数字类型外不一样类型的对象是按照类型的名字来排序的.排序

规则2: 不支持比较操做的相同类型的对象是按照地址来排序的.ci

[我查到stackoverflow(参考2)也有人对这个东西做出补充.]

规则3: 比较两个字符串或两个数字类型, 比较结果是符合预期的(字符串是字典顺序, 整型是数字大小的顺序)

原文: When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

规则4:比较数字类型和非数字类型的时候, 数字类型在前(就是数字类型 < 非数字类型) 

原文: When you order a numeric and a non-numeric type, the numeric type comes first.

规则1的例外: 旧风格的类小于新风格的类.

原文: One exception is old-style classes that always come before new-style classes.

 

咱们能够发现, 其实CPython的强类型不是真的, 存在不少陷进.

下面我对这几个查到的规则进行验证

class Foo(object):
    pass


class Bar(object):
    pass

# 规则1
print Foo() > Bar()

# 规则2
a, b = Foo(), Foo()
print id(a) > id(b), id(a), id(b)
print a > b

# 规则3
print 100 > 1
print 'b' > 'a'


class Foo:
    pass


class Bar(object):
    pass

# 规则4
print Foo > 1000  # classobj > int
f = Foo()
print id(f) < id(1000), id(f), id(1000), id(1000)
print f < 1000  # old-style class instance > int 应该是 Foo() > 1000, 这不符合规则4, Foo是old-style类
print Bar() > 1000  # new-style class instance > int

print 'a' > 1000 # str > int
print {} > 1000  # dict > int
print [] > 1000  # list > int
print (1,) > 1000  # tuple > int

# 规则1的例外
print Foo() < Bar()  # old-style class < new-style class

上面代码的全部比较表达式都是True. 测试环境是2.7.6

我发现这些规则也出现了例外, Foo() > 1000, Foo是old-style类, 是这个缘由么? 不理解, 反正这是个很是困惑的实现方式.

若是有知道的朋友, 麻烦留言告知一下, 这个实现真的是很是困惑. 咱们平常使用的过程当中, 要很是注意判断类型以后再比较, 防止这类陷进.

幸运的是python 3.X已经修正了这个问题, 参考2的例子

>>> '10' > 5
Traceback (most recent call last):
  File "", line 1, in 
    '10' > 5
TypeError: unorderable types: str() > int()

CPython获取对象地址的方法是id(), 官网给出了这样的解释: This is the address of the object in memory.

水平有限, 欢迎拍砖!

 

参考资料:

  1. 麻省理工学院公开课:计算机科学及编程导论 第二课 分支, 条件和循环 (能够在网易公开课中找到)
  2. http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int
  3. http://docs.python.org/2/library/stdtypes.html#comparisons
  4. http://docs.python.org/2/library/functions.html#id
相关文章
相关标签/搜索