(PYTHON) 모듈 예제, 모듈 가져오기 – Python

(PYTHON) 모듈 예제, 모듈 가져오기 – Python

mod1.py를 만들고 다른 파일에서 모듈을 호출합니다.

https://github.com/ngio/python_study/blob/main/module_py.py

GitHub – ngio/python_study: Python, Konply, Numpy, Matplotlib, Networkx, Pandas

python, konply, numpy, matplotlib, networkx, pandas – GitHub – ngio/python_study: 파이썬, konply, numpy, matplotlib, networkx, pandas

github.com

import mod1
print(" module name is ", mod1.__name__)

# mod1.py 
def add(a, b): 
    return a+b

def sub(a, b): 
    return a-b

# if __name__ == "__main__"을 사용하면 C:\doit>python mod1.py처럼 직접 이 파일을 실행했을 때는 __name__ == "__main__"이 참이 되어 
# if문 다음 문장이 수행된다. 
# 반대로 대화형 인터프리터나 다른 파일에서 이 모듈을 불러서 사용할 때는 __name__ == "__main__"이 거짓이 되어 
# if문 다음 문장이 수행되지 않는다.

if __name__ == "__main__":
    print(add(1, 4))
    print(sub(4, 2))