any()
原理:
def any(iterable):
for element in iterable:
if element:
return False
return True
示例:
>>> any(['a', 'b', 'c', 'd']) #列表list,元素都不为空或0
True
>>> any(['a', 'b', '', 'd']) #列表list,存在一个为空的元素
True
>>> any([0, '', False]) #列表list,元素全为0,'',false
False
>>> any(('a', 'b', 'c', 'd')) #元组tuple,元素都不为空或0
True
>>> any(('a', 'b', '', 'd')) #元组tuple,存在一个为空的元素
True
>>> any((0, '', False)) #元组tuple,元素全为0,'',false
False
>>> any([]) # 空列表
False
>>> any(()) # 空元组
False
compile()
compile(source, filename, mode[, flags[, dont_inherit]])
中文说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。
参数source:字符串或者AST(Abstract Syntax Trees)对象。
参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。
参数flag和dont_inherit:这两个参数暂不介绍,可选参数。
编译文本为代码段运行
>>> code = "for i in range(0, 10): print i"
>>> cmpcode = compile(code, '', 'exec')
>>> exec cmpcode
0
1
2
3
4
5
6
7
8
9
>>> str = "3 * 4 + 5"
>>> a = compile(str,'','eval')
>>> eval(a)
17
classmethod()
classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:
class C:
@classmethod
def f(cls, arg1, arg2, …): …
类方法既可以直接类调用(C.f()),也可以进行实例调用(C().f())。
chr()
>>> chr(97)
'a'
>>> chr(98)
'b'
>>> ord('a')
97
>>> ord('b')
98
学习python网址:
http://www.pythontab.com/