아래 내용은 공부한 것을 정리하므로 틀린 내용이 포함되어 있을 수 있습니다.
1. dir 내장 함수를 통한 확인
- Python(파이썬) 기본 - 22. module import와 실행
포스트에서 만든
words.py
파일을 활용하겠습니다. -
REPL을 실행합니다.
words
모듈을 import하고 type을 확인합니다.>>> import words >>> type(words) <class 'module'>
- dir 내장함수는 객체가 가지고 있는 변수나 함수리스트를 보여줍니다.
- 작성했던 함수를 비롯해, import한 모듈, 그리고 더블언더스코어로 이름지어진 특별한 기능을 하는 속성들을 볼 수 있습니다.
>>> dir(words) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fetch_words', 'main', 'print_items', 'sys', 'urlopen']
- 다시 fetch_words의 타입을 확인하면 함수로 확인됩니다.
>>> type(words.fetch_words) <class 'function'>
- 이러한 함수마저도 눈에 보이지 않는 속성들을 가지고 있습니다.
>> dir(words.fetch_words) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
- 함수의 속성들을 몇가지만 확인해보겠습니다.
__name__
: 함수의 명칭을 string으로 리턴해줍니다.>>> words.fetch_words.__name__ 'fetch_words'
__doc__
: 함수의 docstring을 출력해줍니다.>>> words.fetch_words.__doc__ '\n url주소에서 파일을 가져와 단어 리스트를 반환합니다.\n :param url: 불러올 url\n :return:\n '