易截截图软件、单文件、免安装、纯绿色、仅160KB

python比较操作的内幕

今天看了序列类型相关的比较操作.
在python核心编程(2nd)一书中6.13.1章节中, 给出了列表比较的一个准则..
个人感觉还是不很完善:
如果扫描到两个列表中当前比较元素是不可比较的, 那么返回什么??
我用的是python2.6....
对这个问题做了一些测试, 自己目前嘎绝当比较遇到上述情况时, 是使用两个列表的内存地址值来比较的..
以下代码是测试时候的i/o
>>> a = [1, 2, 3, 4]
>>> b = [1, 2, 4, "3"]
>>> a == b
False
>>> a < b
True
>>> a > b
False
>>> a, b = b, a
>>> a
[1, 2, 4, '3']
>>> b
[1, 2, 3, 4]
>>> id(a)
12643064
>>> id(b)
12752576
>>> a < b
False
>>> b < a
True
>>> a = [1, "4"]
>>> b = [2]
>>> a < b
True
>>> id(a)
12642104
>>> id(b)
12643064
>>> c = [0]
>>> a < c
False
>>> id(c)
12752576
>>> a = [1]
>>> b = ["1"]
>>> a < b
True
>>> id(a)
12243072
>>> id(b)
12642104
>>> a = ["1"]
>>> b = [1]
>>> a < b
False
>>> id(a)
12643064
>>> id(b)
12243072
>>>


相关文档:

Python入门的36个例子 之 26

源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()

output:
>>>
This is the first line.
This is the second line.
This is ......

Python入门的36个例子 之 30

源代码下载:下载地址在这里
# 034
class Person:
def __init__(self, name):
self.name = name # 这一句中第一个name是类中包含的域,第二个name是传进来的参数
# end of def
def sayHello(self):
print 'Hello!'
# end of def
# end of class
p = Person('Ning')
print p.nam ......

Python入门的36个例子 之 31

源代码下载:下载地址在这里
# 035
class Person:
population = 0 #这个变量是属于整个类的
def __init__(self, name):
self.name = name
print '初始化 %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self. ......

Python入门的36个例子 之 32

源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......

Python入门的36个例子 之 33

源代码下载:下载地址在这里
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号