這會是很長的一個帖子,因為我打算從python最基礎的東西開始,嘗試去完全的掌握它,buildin中有一些常用的函數比如 abs, open, setattr, getattr, 大家都很了解他們的用法,因為平時用的比較多,這將把重點放在平時少用,但是有奇效的方法,比如說 enumerate, 這個方法在遍歷列表和元組的時候非常有用,下面我會詳細說明這類方法的用法和作用。
Help on built-in function abs in module __builtin__abs(...) abs(number) -> number Return the absolute value of the argument.這個方法很簡單,只有一個參數,就是用來返回它的絕對值。
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. help中已經說的很清楚了,這個方法接受一個iterable,并且自動判斷其中的item是不是都為true,如果都為true的話,該方法就返回true,如果該iterable是空,那么也返回true。
比較重要的一點是,該方法的判斷的依據,如果不清楚判斷依據的話,那沒人知道什么時候會返回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; for (;;) { 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_RETURN_FALSE; } } Py_DECREF(it); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); else return NULL; } Py_RETURN_TRUE;}上面是 bltinmodule.c 的源碼,從源碼可以看出,其中調用了bool(x)來對序列中的每一項進行求值,如果該參數不可以iterable,則返回NULL,如果獲取的迭代器為空, 并且沒有異常發(fā)生,則返回True,從上面的代碼可以看出bool()其實就是 int PyObject_IsTrue(PyObject *o) , 這個方法是python的capi中的一個協(xié)議, 如果python認為它是True的話就返回1,否則返回0 ,詳情可以去查看它的源碼。
any(...)any(iterable) -> boolReturn True if bool(x) is True for any x in the iterable.If the iterable is empty, return False.這個方法也很簡單,但是很實用,當序列的迭代器中只有一個item返回為true的時候,該方法就返回true。
和all方法不同的是, 當迭代器為空時, any返回的時False,而all返回的是True, 這一點讓我不得其解。
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
這是一個抽象類,但是它卻定義在了builtin function里面, 抽象類是不可以被實例化的,所以它在這里被定義的唯一的作用就是,讓我們在判斷字符串類型的時候更加方便。
python2中有2類字符串,一種是 unicode, 另一種是 str, 所以在判斷對象是不是字符串的時候最好使用
isinstance(target_str, basestring)這樣就不用if else判斷了,可以節(jié)省一些cpu。
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
| object
bool是很常用的類型,bool采用python中的標準truth測試進行判定一個值的布爾狀態(tài),這個測試就是 PyObject_IsTrue , bool是int的子類,并且bool不能被再次繼承,如果bool的參數為空,那么它將返回false。
要分清楚builtin function 中的 bool()方法, 和bool類的區(qū)別。
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.這個方法是非常常用的方法,當你要利用python的動態(tài)特性去做一些功能的時候這個方法就很方便,這個方法嘗試去call參數中的對象,如果可以調用,那么返回true,不可以被調用,那么返回false,即使返回true,該方法也有可能調用失敗,所以這個callable并不是真正的去調用了參數一次,而是看參數是否能被調用,這里需要注意一下。
其次類也是可以被調用的,類的實例也可以被調用,只要它實現了 __call__() 這個魔術方法 , 類實現這個魔術方法以后,在調用的時候,世紀上調用的是這個魔術方法。
新聞熱點
疑難解答