2. 內建函式¶
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.
-
all(iterable)¶ Return
Trueif all elements of the iterable are true (or if the iterable is empty). Equivalent to:def all(iterable): for element in iterable: if not element: return False return True
-
any(iterable)¶ Return
Trueif any element of the iterable is true. If the iterable is empty, returnFalse. Equivalent to: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 byrepr()using\x,\uor\Uescapes. This generates a string similar to that returned byrepr()in Python 2.
-
bin(x)¶ Convert an integer number to a binary string prefixed with 「0b」. The result is a valid Python expression. If x is not a Python
intobject, it has to define an__index__()method that returns an integer. Some examples:>>> bin(3) '0b11' >>> bin(-10) '-0b1010'
If prefix 「0b」 is desired or not, you can use either of the following ways.
>>> format(14, '#b'), format(14, 'b') ('0b1110', '1110') >>> f'{14:#b}', f'{14:b}' ('0b1110', '1110')
See also
format()for more information.
-
class
bool([x])¶ Return a Boolean value, i.e. one of
TrueorFalse. x is converted using the standard truth testing procedure. If x is false or omitted, this returnsFalse; otherwise it returnsTrue. Theboolclass is a subclass ofint(see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances areFalseandTrue(see Boolean Values).
-
breakpoint(*args, **kws)¶ This function drops you into the debugger at the call site. Specifically, it calls
sys.breakpointhook(), passingargsandkwsstraight through. By default,sys.breakpointhook()callspdb.set_trace()expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly importpdbor type as much code to enter the debugger. However,sys.breakpointhook()can be set to some other function andbreakpoint()will automatically call that, allowing you to drop into the debugger of choice.3.7 版新加入.
-
class
bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The
bytearrayclass 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 thebytestype 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 usingstr.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 initialize the bytes array.
- If it is an iterable, it must be an iterable of integers in the range
0 <= x < 256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
See also Binary Sequence Types — bytes, bytearray, memoryview and Bytearray Objects.
- If it is a string, you must also give the encoding (and optionally,
errors) parameters;
-
class
bytes([source[, encoding[, errors]]]) Return a new 「bytes」 object, which is an immutable sequence of integers in the range
0 <= x < 256.bytesis an immutable version ofbytearray– it has the same non-mutating methods and the same indexing and slicing behavior.Accordingly, constructor arguments are interpreted as for
bytearray().Bytes objects can also be created with literals, see String and Bytes literals.
See also Binary Sequence Types — bytes, bytearray, memoryview, Bytes Objects, and Bytes and Bytearray Operations.
-
callable(object)¶
