[Python] builtin abs() 함수

2021. 1. 7. 18:04·Python/Python
728x90
반응형

abs(x) 함수는 입력 받은 값의 절대값을 return 해주는 함수이다.

정의

def abs(*args, **kwargs): # real signature unknown
    Return the absolute value of the argument.

실행결과

sample = [
    10, - 10,
    10.5, - 10.5,
    0, 0.0,
]
for s in sample:
    print('{} => {}'.format(s, abs(s)))

"""
10    => 10
-10   => 10
10.5  => 10.5
-10.5 => 10.5
0     => 0
0.0   => 0.0
"""

이런식으로도 사용이 가능하다.

from operator import __abs__

__abs__(10)

구현단

PyObject *
PyNumber_Absolute(PyObject *o)
{
    PyNumberMethods *m;

    if (o == NULL) {
        return null_error();
    }

    m = Py_TYPE(o)->tp_as_number;
    if (m && m->nb_absolute)
        return m->nb_absolute(o);

    return type_error("bad operand type for abs(): '%.200s'", o);
}
tp_as_number


PyNumberMethods* PyTypeObject.tp_as_number

Pointer to an additional structure that contains fields relevant only to objects which implement the number protocol. These fields are documented in Number Object Structures.

Inheritance:
The tp_as_number field is not inherited, but the contained fields are inherited individually.

nb_absolute

 

728x90
반응형
저작자표시 (새창열림)
'Python/Python' 카테고리의 다른 글
  • [Python] 검색 방법 profile 해보기
  • CentOS pyenv 설치
  • [Python] builtin dir() 함수
  • [Python] builtin all() 함수
상쾌한기분
상쾌한기분
    반응형
    250x250
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (252) N
      • 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) N
        • 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 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바