python中的buildin函数详解(第一篇)

这会是很长的一个帖子,因为我打算从python最基础的东西开始,尝试去完全的掌握它,buildin中有一些常用的函数比如 abs, open, setattr, getattr, 大家都很了解他们的用法,因为平时用的比较多,这将把重点放在平时少用,但是有奇效的方法,比如说 enumerate, 这个方法在遍历列表和元组的时候非常有用,下面我会详细说明这类方法的用法和作用。

abs(x)
Help on built-in function abs in module __builtin__
abs(...)
  abs(number) -> number
  Return the absolute value of the argument.

这个方法很简单,只有一个参数,就是用来返回它的绝对值。

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.
  

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,如果获取的迭代器为空, 并且没有异常发生,则返回True,从上面的代码可以看出bool()其实就是 int PyObject_IsTrue(PyObject *o) , 这个方法是python的capi中的一个协议, 如果python认为它是True的话就返回1,否则返回0 ,详情可以去查看它的源码。

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.

这个方法也很简单,但是很实用,当序列的迭代器中只有一个item返回为true的时候,该方法就返回true。
和all方法不同的是, 当迭代器为空时, any返回的时False,而all返回的是True, 这一点让我不得其解。

basestring()
class basestring(object)
|  Type basestring cannot be instantiated; it is the base for str and unicode.
|  
|  Data and other attributes defined here:
|  
|  __new__ = <built-in method __new__ of type object>
|      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判断了,可以节省一些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
|      object

bool是很常用的类型,bool采用python中的标准truth测试进行判定一个值的布尔状态,这个测试就是 PyObject_IsTrue , bool是int的子类,并且bool不能被再次继承,如果bool的参数为空,那么它将返回false。

要分清楚builtin function 中的 bool()方法, 和bool类的区别。

callable(object)

callable(...)

callable(object) -> bool
Return whether the object is callable (i.e., some kind of function).
Note that classes are callable, as are instances with a __call__() method.

这个方法是非常常用的方法,当你要利用python的动态特性去做一些功能的时候这个方法就很方便,这个方法尝试去call参数中的对象,如果可以调用,那么返回true,不可以被调用,那么返回false,即使返回true,该方法也有可能调用失败,所以这个callable并不是真正的去调用了参数一次,而是看参数是否能被调用,这里需要注意一下。
其次类也是可以被调用的,类的实例也可以被调用,只要它实现了 __call__() 这个魔术方法 , 类实现这个魔术方法以后,在调用的时候,世纪上调用的是这个魔术方法。

转载于:https://www.cnblogs.com/youngershen/p/3985089.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值