参考Python Built-in Function
abs(x)
输入:整数或者浮点数
输出:绝对值
all(iterable)
如果输入的可迭代对象中的元素都为true(或者这个对象没有元素),那么函数返回True
>>> list=[]
>>> all(list)
True
any(iterable)
如果可迭代对象中的元素存在为true,那么函数返回True,如果对象为没有元素,那么返回False
>>> list=[]
>>> any(list)
False
bin(x)
将一个数转换为二进制字符串,如果x不是int,那么需要定义__index__()方法返回一个整数
>>> bin(10)
'0b1010'
>>>bin(12.21)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
class bool([x])
判断x是否符合以下条件,返回False
- None
- False
- 任何数字类型的0,比如0,0.0,0j
- 任何空序列比如”,(),[]
- 任何空字典比如{}
- 自定义类中定义了__bool__()或者__len__()方法,方法返回一个0或者False
repr(object)
参考
>>> repr('hello')
"'hello'"
>>> repr('你你好')
"'你你好'"
ascii(object)
作用和repr()函数相似,返回一个字符串,字符串包含object的可打印表达,但是非ASCII字符会使用\x,\u,\U转义
>>> ascii('你好')
"'\\u4f60\\u597d'"
>>> ascii('hello')
"'hello'"
eval(expression,globals=None,locals=None)
执行expression,使用globals(dictionary)和locals(mapping object)传入全局和局部变量,
提示:可以使用globals()和locals()返回现在global和local的dictionary
>>> x = 1
>>> eval('x+1')
2
>>> locals()
{'list': [], '__name__': '__main__', '__doc__': None, '__spec__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__builtins__': <module 'builtins' (built-in)>, '__package__': None}
>>> globals()
{'list': [], '__name__': '__main__', '__doc__': None, '__spec__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__builtins__': <module 'builtins' (built-in)>, '__package__': None}
class bytearray([source[,encoding[,errors]]]
创建一个字节数组对象,可以方便的使用一些函数,Bytes and Bytearray Operations
>>> c=bytearray('你好','utf-8')
>>> c
bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
class bytes([source[,encoding[,errors]]]
返回一个不可变字节序列
>>> c=bytes('你好','utf-8')
>>> c
b'\xe4\xbd\xa0\xe5\xa5\xbd'
callable(object)
如果object可以调用,那么返回True
>>> def printhello():
... print("hello")
...
>>> callable(printhello)
True
chr(i)
返回i(范围是0~1,114,111(0x10FFFF))对应的字符
>>> chr(8364)
'€'
如果出现这种情况请设置环境编码格式
>>> chr(8364)
'\u20ac'
ord(c)
返回字符的Unicode整数值
>>> ord('欧')
27431
classmethod(function)
使类可以直接调用该函数
>>> class C:
... @classmethod
... def f(self):
... print("hello")
>>>
>>> C.f()
hello
>>> class B:
... def f(self):
... print("hello")
...
>>> B.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'self'
compile(source,filename,mode,flag=0,dont_inherit=False,optimize=-1)
编译source成一个可以被exec()和eval()执行的Code object
>>> str = '[i for i in range(10)]'
>>> a=compile(str,'','eval')
>>> eval(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
delattr(object, name)
删除object的属性
divmod(a,b)
返回商和余数,余数和b同号
>>> divmod(8,4)
(2, 0)
>>> divmod(8,-2.5)
(-4.0, -2.0)
enumerate(iterable, start=0)
对象要可迭代
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
filter(function,iterable)
创建一个迭代器,item for item in iterable if function(item)
>>> a=[1,2,3,4]
>>> list(filter(lambda x : not x%2 ,a))
[2, 4]
相反:itertools.filterfalse(predicate,iterable)
如果函数返回False,则被添加
>>> import itertools
>>> list(itertools.filterfalse(lambda x:x%2,range(10)))
[0, 2, 4, 6, 8]
class float([x])
>>> float('+1.23')
1.23
>>> float(' -12345\n')
-12345.0
>>> float('1e-003')
0.001
>>> float('+1E6')
1000000.0
>>> float('-Infinity')
-inf
>>> float()
0.0
zip(*iterables)
按照最短原则依次连接各个序列的元素,返回一个元组迭代器
>>> list(zip([1,2],[1,2,3],[1,2]))
[(1, 1, 1), (2, 2, 2)]
>>> list(zip('nihao','wodhi'))
[('n', 'w'), ('i', 'o'), ('h', 'd'), ('a', 'h'), ('o', 'i')]
在传入序列前使用*操作符,作用相当于把序列的元素传入
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>>zip(*zip(x, y))
>zip(*[(1,4),(2,5),(3,6)])
>zip((1,4),(2,5),(3,6))
>[(1,2,3),(4,5,6)]