问题描述
在许多代码中,我看到其中包含函数的类,它们只是使用了 pass
短语并对其进行了一些注释.就像这个来自 python 的本机内置函数:
in many codes, i see classes with functions in them that they just used pass
phrase with some comment upon them.like this native builtin function from 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
我知道 pass 什么都不做,它的那种冷漠和 null
短语,但为什么程序员使用这样的函数?
i know pass does nothing, and its kind of apathetic and null
phrase, but why programmers use such functions ?
还有一些带有 return ""
的函数,例如:
and also there are some functions with return ""
like:
def bin(number): # real signature unknown; restored from __doc__
"""
bin(number) -> string
Return the binary representation of an integer.
>>> bin(2796202)
'0b1010101010101010101010'
"""
return ""
程序员为什么要使用这些东西?
why programmers use such things ?
推荐答案
你的 IDE 在骗你.这些函数实际上看起来不像那样;您的 IDE 编写了一堆与真实事物几乎没有相似之处的虚假源代码.这就是为什么它会说# real signature unknown
之类的东西.我不知道为什么他们认为这是个好主意.
Your IDE is lying to you. Those functions don't actually look like that; your IDE has made up a bunch of fake source code with almost no resemblance to the real thing. That's why it says things like # real signature unknown
. I don't know why they thought this was a good idea.
真正的代码看起来完全不同.例如,这里是真正的 bin
(Python 2.7 版本):
The real code looks completely different. For example, here's the real bin
(Python 2.7 version):
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.");
它是用 C 编写的,它是作为 C 函数的简单包装器实现的 PyNumber_ToBase
:
It's written in C, and it's implemented as a simple wrapper around the C function 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;
}
这篇关于带通行证的python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!