Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python function with a pass in it [duplicate]

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

i know pass does nothing, and its kind of apathetic and null phrase, but why programmers use such functions ?

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 ?

like image 913
anon Avatar asked Jan 05 '23 13:01

anon


1 Answers

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.

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.");

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;
}
like image 200
user2357112 supports Monica Avatar answered Jan 14 '23 01:01

user2357112 supports Monica