带有传递的python函数

ano*_*non 3 python null python-2.x python-3.x

在许多代码中,我看到其中包含函数的类,他们只是使用了pass短语并对其进行了一些评论.像python中的本机内置函数:

def copyright(*args, **kwargs): # real signature unknown
"""
interactive prompt objects for printing the license text, a list of
    contributors and the copyright notice.
"""
pass
Run Code Online (Sandbox Code Playgroud)

我知道传球无所作为,它的那种冷漠和null短语,但为什么程序员使用这样的功能?

还有一些功能return ""如下:

def bin(number): # real signature unknown; restored from __doc__
"""
bin(number) -> string

Return the binary representation of an integer.

   >>> bin(2796202)
   '0b1010101010101010101010'
"""
return ""
Run Code Online (Sandbox Code Playgroud)

为什么程序员会使用这些东西?

use*_*ica 6

你的IDE骗你.那些功能实际上并不像那样; 你的IDE组成了一堆虚假的源代码,与真实的东西几乎没有任何相似之处.这就是为什么它说的话# real signature unknown.我不知道为什么他们认为这是个好主意.

真正的代码看起来完全不同.例如,这是真实的bin(Python 2.7版本):

static PyObject *
builtin_bin(PyObject *self, PyObject *v)
{
    return PyNumber_ToBase(v, 2);
}

PyDoc_STRVAR(bin_doc,
"bin(number) -> string\n\
\n\
Return the binary representation of an integer or long integer.");
Run Code Online (Sandbox Code Playgroud)

它是用C语言编写的,它是作为C函数的简单包装器实现的PyNumber_ToBase:

PyObject *
PyNumber_ToBase(PyObject *n, int base)
{
    PyObject *res = NULL;
    PyObject *index = PyNumber_Index(n);

    if (!index)
        return NULL;
    if (PyLong_Check(index))
        res = _PyLong_Format(index, base, 0, 1);
    else if (PyInt_Check(index))
        res = _PyInt_Format((PyIntObject*)index, base, 1);
    else
        /* It should not be possible to get here, as
           PyNumber_Index already has a check for the same
           condition */
        PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
                        "int or long");
    Py_DECREF(index);
    return res;
}
Run Code Online (Sandbox Code Playgroud)


Nul*_*man 3

这是一件 TBD(待完成)的事情,你知道你将需要这个函数,你知道要给它什么,你知道它返回什么,但你现在不打算写它,所以你做了一个“原型”

有时软件包会附带这些函数,因为它们希望您继承它们并覆盖它们

  • 看似合理的猜测,但真正的原因却完全不同。请注意,这些是内置对象,例如内置的“bin”函数或存在的“copyright”对象,因此您可以在交互模式下键入“copyright”并获取版权消息。它们已经实施了。 (3认同)