파이썬은 python 명령어로 실행하는 실제 파일 이름을 __main__으로 정한다.
그 다음 module을 import해서 모듈 코드를 읽어 들인다.
코드 진입점에 해당하는 파일을 지칭함으로써 실제 실행되는 파일이 무엇인지 나타내기 위한 특별한 모듈 이름이다.
__name__ 변수 값이 __main__으로 지정되는 경우를 별도로 지정하면, 라이브러리로 사용하는 경우와 직접 모듈을 실행하는 경우로 나누어 코드를 작성할 수 있다.
예를 들어, main.py 함수를 실행할 때는 영향을 받지않고, 특정 모듈을 실행했을 경우에만 작동하는 테스트를 진행하도록 만들 수도 있다.
Code
# main.py
from calcuator import calcuator_sum
print("main.py starting....")
print(caculate_sum(1,2))
# calculator.py
def calcuate_sum(a, b):
return a + b
if __name__ == '__main__':
print("breakpoint testing....")
assert calcurate_sum(10,20) == 40
Terminal result
$ python calculator.py
breakpoint testing....
AssertionError
$ python main.py
main.py starting....
3
reference
'Python' 카테고리의 다른 글
| [Clean Python] 파이썬에서의 underscore(_, __)에 대해 (0) | 2024.11.23 |
|---|---|
| [Python] Mac에서 default Python 버젼 변경하기 (0) | 2024.11.22 |
| [Pandas] FutureWarning 에러 - Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. (0) | 2024.11.15 |
| [Python] __init__.py 파일에 대해 알아보자. (1) | 2024.11.14 |
| [Python] __repr__ 메소드에 대해 알아보자 (0) | 2024.11.11 |