Python开发 標準內建方法 (未完代補)

 

abs(number)  絕對值 

The abs() method takes a single argument:

   num - number whose absolute value is to be returned. The number can be:

  1. integer
  2. floating number
  3. complex number

any(iterable)  

The any() method returns True if any element of an iterable is True. If not, any() returns False.

*iterable(list, string, dictionary, tumple ...)

  • True if at least one element of an iterable is true
  • False if all elements are false or if an iterable is empty  (表 "0", "False", empty

all(iterable)

The all() method returns True when all elements in the given iterable are true. If not, it returns False.

ascii(object)   ascii編碼(拉丁字母)

The ascii() method returns a string containing a printable representation of an object. It escapes the non-ASCII characters in the string using x, u or U escapes.

*object (like: stringslist etc).

bin(num)  轉二進制

The bin() method converts and returns the binary equivalent string of a given integer. If the parameter isn't an integer, it has to implement __index__() method to return an integer.

  • num - an integer number whose binary equivalent is to be calculated.
    • If not an integer, should implement __index__() method to return an integer
number = 5
print('The binary equivalent of 5 is:', bin(number))

The binary equivalent of 5 is: 0b101

bool([value])  轉布爾值

The bool() method converts a value to Boolean (True or False) using the standard truth testing procedure.

  The following values are considered false in Python:

  • None
  • False
  • Zero of any numeric type. For example, 00.00j
  • Empty sequence. For example, ()[]''.
  • Empty mapping. For example, {}
  • objects of Classes which has __bool__() or __len()__ method which returns 0 or False

bytearray([source[, encoding[, errors]]])

The bytearray() method returns a bytearray object which is a mutable (can be modified) sequence of integers in the range 0 <=x < 256.

If you want the immutable version, use bytes([source[, encoding[, errors]]]) method.

callable(object)  object是否定義過
The callable() method returns True if the object passed appears callable. If not, it returns False.

chr(i)  回傳unicode的對應值

The chrt() method returns a character (a string) from an integer (represents unicode code point of the character).

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1

The compile() method returns a Python code object from the source (normal string, a byte string, or an AST object).

The code object returned by the compile() method can later be called using methods like: exec() and eval() which will execute dynamically generated Python code.

 exec(object, globals, locals)

The exec() method executes the dynamically created program, which is either a string or a code object.

 eval(expression, globals=None, locals=None)

The eval() method parses the expression passed to this method and runs python expression (code) within the program.

 

 classmethod(function)

 The classmethod() method returns a class method for the given function.

  • function - Function that needs to be converted into a class method

@classmethod   高端裝飾,接在 def Person.printAge()之前,效果同等 Person.printAge = classmethod(Person.printAge)







未完待續

https://www.programiz.com/python-programming/methods/built-in/classmethod

原文地址:https://www.cnblogs.com/pyleu1028/p/9886677.html