[Python] builtin all() 함수

2021. 1. 8. 11:16·Python/Python
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
반응형
저작자표시 (새창열림)
'Python/Python' 카테고리의 다른 글
  • [Python] 검색 방법 profile 해보기
  • CentOS pyenv 설치
  • [Python] builtin dir() 함수
  • [Python] builtin abs() 함수
상쾌한기분
상쾌한기분
    반응형
    250x250
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (252)
      • Python (44)
        • Python (26)
        • Django (6)
        • Flask (4)
        • Open Source (6)
      • Kotlin & Java (5)
        • Spring (2)
        • 프로젝트 (1)
      • Go (11)
      • Database (24)
        • MySQL (21)
        • Redis (3)
      • Infrastructure (2)
        • CDC (4)
        • Kafka (5)
        • Prometheus (2)
        • Fluentd (11)
        • Docker (1)
        • Airflow (2)
        • VPN (2)
      • IT (27)
        • AI (9)
        • Langchain (8)
        • Web (18)
        • Git (8)
        • 리팩토링 (9)
        • Micro Service Architecture (8)
        • Clean Code (16)
        • Design Pattern (0)
        • 수학 (1)
        • 알고리즘 (14)
      • OS (14)
        • Centos (10)
        • Ubuntu (3)
        • Mac (1)
      • Search Engine (2)
        • ElasticSearch (1)
        • Lucene Solr (1)
      • PHP (2)
        • Laravel (1)
        • Codeigniter (1)
  • 블로그 메뉴

    • Github 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    오블완
    Kafka
    prompt
    go
    http
    티스토리챌린지
    LLM
    파이썬
    Golang
    CDC
    MYSQL
    백준
    ollama
    Redis
    performance
    python
    git
    Langchain
    fluentd
    docker
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
[Python] builtin all() 함수
상단으로

티스토리툴바