易截截图软件、单文件、免安装、纯绿色、仅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个例子 之 24

# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # 返回首次出现“gl”字符串的索引
if toolName.find('Baidu ......

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个例子 之 36

# 040
import time
try:
f = file('040_Finally.py')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(0.33)
print line,
# end of while
finally:
f.close()
print 'Closed the file.'
# end of try
output:
> ......

Python Firewall Win32 (pyfw

Python Firewall Win32 (pyfw-win32)
pyfw-win32是一个可用Python脚本开发数据包过滤(防火墙)的模块。底层使用C语言编写的NDIS中间层驱动(NDIS IMD)提供支持,上层提供Python开发接口。可用Python脚本处理所有逻辑问题,而不必关心底层实现,达到快速、灵活开发的目的。
Google 项目托管:
http://code.google.com/p/py ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号