Python 内置函数
abs(x)
Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
返回一个数的绝对值。参数可以是整形或资格浮点数。如果参数是复数,则该复数的模(|z|=√a²+b²,)被返回。
For example:
print abs(1) # 1
print abs(-1) # 1
print abs(1.0) # 1.0
print abs(-10) # 1.0
print abs(3+4j) # 5
all(iterable)
Return True if all elements of the iterable are true (or if the iterableis empty). Equivalent to:
如果可迭代对象的所有元素都为True或者该对象为空则返回真, 否则返回False。相当于:
def all(iterable):
for element in iterable:
if not element:
return False
return True
any(iterable)
Return True if any element of the iterable are true, if the iterable is empty(all elements of the iterable are false), return False. Equivalent to:
如果可迭代对象的任一元素为true则返回True, 如果可迭代对象的所有元素为false或者该对象为空则返回False。相当于:
def any(iterable):
for element in iterable:
if element:
return True
return False
ascii(object)
As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u or \U escapes.
This generates a string similar to that returned by repr() in Python 2.
类似于repr(),返回一个对象的可打印的字符串表示,在字符串中的转义非ASCI字符通过repr()函数使用\x, \u, \U转义字符返回,产生的字符串和Python2中的repr()函数
类似。(换句话说就是将对象转化为一个ASCII字符表示的字符串。)
ascii("哇哈哈abc") # "'\\u54c7\\u54c8\\u54c8abc'"
repr("哇哈哈abc") # "'哇哈哈abc'"
bin(x)
class Test:
__init__(self, num):
self.num = num
__index__(self):
return self.num
bin(10) # '0b1010'
bin(A(10)) # '0b1010'
bool([x])
Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is 将一个值使用标准的真值测试过程转化为Boolean类型. 如果x为false或者x为缺省值, 返回False,否则返回True.另外bool是一个int类型的子类.bool不能被进一步的继承.
它只有两个实例True和False(详见Boolean值).
bool(1) # True
bool(0) # False
bool(-1) # True
bool() # False
bytearray([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Bytearray Operations.
The optional source parameter can be used to initialize the array in a few different ways:
If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to in