Detailed description of buildin functions in python (Article 1)
This will be a long post, because I plan to start from the most basic things of python and try to fully master it. buildin has some common functions such as abs, open, setattr, getattr, everyone knows their usage very well, because they usually use a lot of resources, which will focus on less use at ordinary times, but there are miraculous methods, such as enumerate, this method is very useful when traversing lists and metadata sets. I will describe the usage and function of this method in detail below. Abs (x) Help on built-in function abs in module _ builtin _ abs (...) abs (number)-> number Return the absolute value of the argument. this method is very simple. There is only one parameter, which is used to return its absolute value. All (iterable) Help on built-in function all in module _ builtin __: all (...) all (iterable)-> bool Return True if bool (x) is True for all values x in the iterable. if the iterable is empty, return True. this method accepts an iterable and automatically determines whether all items are true. If all items are true, this method returns true, if the iterable is null, true is returned. The more important thing is the basis for the judgment of this method. If the judgment is unclear, no one knows when to return false. Static PyObject * builtin_all (PyObject * self, PyObject * v) {PyObject * it, * item; PyObject * (* iternext) (PyObject *); int cmp; it = PyObject_GetIter (v); if (it = NULL) return NULL; iternext = * Py_TYPE (it)-> tp_iternext; (;;) {item = iternext (it); if (item = NULL) break; cmp = PyObject_IsTrue (item); Py_DECREF (item); if (cmp <0) {Py_DECREF (it); return NULL;} if (cmp = 0) {Py_DECREF (it); Py_RETUR N_FALSE ;}} Py_DECREF (it); if (PyErr_Occurred () {if (condition (PyExc_StopIteration) PyErr_Clear (); else return NULL;} Py_RETURN_TRUE;} The above is bltinmodule. c source code. We can see from the source code that bool (x) is called to evaluate each item in the sequence. If this parameter cannot be iterable, NULL is returned, if the obtained iterator is empty and no exception occurs, True is returned. From the code above, we can see that bool () is actually int PyObject_IsTrue (PyObject * o ), this method is a protocol in python capi. If python considers it to be True, 1 is returned; otherwise, 0 is returned. For details, you can view its source code. Any (iterable) any (...) any (iterable)-> bool Return True if bool (x) is True for any x in the iterable. if the iterable is empty, return False. this method is also very simple, but very practical. When only one item in the series iterator returns true, this method returns true. Different from the all method, when the iterator is empty, any returns False, and all returns True, which cannot be solved. Basestring () class basestring (object) | Type basestring cannot be instantiated; it is the base for str and unicode. | Data and other attributes defined here: | new = | T. new (S ,...) -> a new object with type S, a subtype of T is an abstract class, but it is defined in builtin function. abstract classes cannot be instantiated, therefore, the only function defined here is to make it easier for us to determine the string type. Python2 has two types of strings, unicode and str. Therefore, it is best to use isinstance (target_str, basestring) when determining whether the object is a string) in this way, you do not need to judge if else, which can save some cpu. Bool ([x]) Returns True when the argument x is true, False otherwise. | The builtins True and False are the only two instances of the class bool. | The class bool is a subclass of the class int, and cannot be subclassed. | Method resolution order: | bool | int | the object bool is a common type. bool uses the standard truth test in python to determine the Boolean state of a value. This test is PyObject_IsTrue, bool is a subclass of int, and bool cannot be inherited again. If bool is null, it returns false. The bool () method in builtin function is distinguished from the bool class. Callable (object) callable (...) callable (object)-> boolReturn whether the object is callable (I. e ., some kind of function ). note that classes are callable, as are instances with a _ call _ () method. this method is a very common method. This method is very convenient when you want to use the dynamic characteristics of python to do some functions. This method tries to get the object in the call parameter, if yes, true is returned and cannot be called. If yes, false is returned. Even if true is returned, this method may fail to be called, therefore, this callable does not actually call the parameter once, but checks whether the parameter can be called. Pay attention to it here. The second class can also be called, and the instance of the class can also be called, as long as it implements the _ call _ () magic method, after the class implements this magic method, this magic method was called in the past century.