Python/Python

[Python] builtin all() 함수

상쾌한기분 2021. 1. 8. 11:16
728x90
반응형

정의

def all(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.
    """
    pass

all() 함수는 iterable 안의 값이 모두 참이거나 empty 라면 return True.
그렇지 않다면 return False 를 한다.

실행결과

sample = [
    [],
    [0],
    ['0'],
    [1],
    ['1'],
    [0, 1, 2],
    [1, 2, 3],
    [-0],
    [-1],
    [True],
    [False],
]

for s in sample:
    print(s, '=>', all(s))

"""
[] => True
[0] => False
['0'] => True
[1] => True
['1'] => True
[0] => False
[-1] => True
[0, 1, 2] => False
[1, 2, 3] => True
[True] => True
[False] => False
"""

[] => True
[]는 empty 이므로 True

[0] => False
0은 Python에서 False 로 취급

['0'] => True
string은 Python에서 True로 취급

[1] => True
1은 Python에서 True로 취급

['1'] => True
string은 Python에서 True로 취급

[-0] => False
0은 Python에서 False로 취급

[-1] => True
1은 Python에서 True로 취급
(음수는 양수로 변환한다)

[0, 1, 2] => False
0은 Python에서 False 로 취급

[1, 2, 3] => True
1은 Python에서 True 로 취급

[True] => True

[False] => False


구현단

static PyObject *
builtin_all(PyObject *module, PyObject *iterable)
/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
{
    PyObject *it, *item;
    PyObject *(*iternext)(PyObject *);
    int cmp;

    /* iterable 이 아니라면 False (NULL은 0의 값을 가지고 있습니다.) */
    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;
    iternext = *Py_TYPE(it)->tp_iternext;

    for (;;) {
        item = iternext(it);
        if (item == NULL)
            break;

        /* PyObject_IsTrue()를 통해서 cmp 값 비교 */
        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;
    }

    /* IF문 걸리지 않았다면 Return True */
    Py_RETURN_TRUE;
}

 

객체를 True 로 판단한다면 Return 1, 
그렇지 않다면 Return 0.

int
PyObject_IsTrue(PyObject *v)
{
    Py_ssize_t res;
    if (v == Py_True)
        return 1;
    if (v == Py_False)
        return 0;
    if (v == Py_None)
        return 0;
    else if (Py_TYPE(v)->tp_as_number != NULL &&
             Py_TYPE(v)->tp_as_number->nb_bool != NULL)
        res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
    else if (Py_TYPE(v)->tp_as_mapping != NULL &&
             Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
        res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
    else if (Py_TYPE(v)->tp_as_sequence != NULL &&
             Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
        res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
    else
        return 1;
    /* if it is negative, it should be either -1 or -2 */
    return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
}
728x90
반응형