파이썬내장함수 : dir() 함수

Sunghee Park·2022년 10월 18일
0

python_grammar

목록 보기
1/1

1.1 dir()

네임 스페이스[: 변수명이나 함수명 그리고 클래스의 이름을 언어 차원에서 관리해주는 매커니즘]
에 등록되어 있는 모든 이름들을 리스트로 바노한해주는 파이썬의 내장 함수

A = 1
B = 2
dir()
#['A', 'B', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

1.2 __ 자동으로 등록된 특수한 이름들

파이썬 인터프리터를 실행할 때 자동으로 등록된 특수한 목적이 있는 이름들

dir()
#['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

ex) name변수 이름에는 현재 프로그램이 실행되고 있는 모듈의 이름이 저장 되어 있음

__name__
#'__main__'

1.3 특정 모듈의 네임 스페이스 엿보기

math 모듈에서 어떤 변수와 함수를 제공하는지 확인하기 위해 math 모듈 임포트 후 dir() 함수를 호출해보면 모듈의 이름들이 네임 스페이스에 추가된 것을 확인 가능

import math
dir()
#['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math']

math 모듈이 제공하는 모든 기능들 확인 : dir() 함수의 인자로 math 넘김

dir(math)
#['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

reference

https://www.daleseo.com/python-dir/

0개의 댓글