스터디/혼자공부하는파이썬
chapter07. 모듈(3)
냠냠쿠
2024. 4. 30. 20:04
728x90
1. 모듈 만들기
import test_module as test
radius = test.number_input()
print(test.get_circumference(radius))
(1) __name__ == "__main__"
1) __name__
프로그램 진입점을 엔트리 포인트 또는 메인이라고 부르는데, 이러한 엔트리 포인트 또는 메인 내부에서의 __name__은 "__main__"이다.
2) 모듈의 __name__
모듈 내부에서 __name__을 출력하면 모듈의 이름을 나타냄
import test_module
print(__name__) #test_module
3) __name__ 활용하기
현재 파일이 모듈로 모듈로 실행되는지 엔트리 포인트로 실행되는지 알 수 있다.
pi = 3.141592
def number _input():
output = input("숫자입력>>>")
return float(output)
def get_circumference(radius):
return 2* pi * radius
def get_circle_area(radius):
return pi*radius * radius
print("get_circumference(10):", get_circumference(10))
print("get_circle_area(10):", get_circle_area(10))
import test_module as test
radius = test.number_input()
print(test.get_circumference(radius)) #62.83184
print(test.get_circle_area(radius)) # 314.1592
4) 엔트리를 활용하는 모듈 만들기
pi = 3.141592
def number _input():
output = input("숫자입력>>>")
return float(output)
def get_circumference(radius):
return 2* pi * radius
def get_circle_area(radius):
return pi*radius * radius
if __name__ == "__main__"": #엔트리포인트 일때만 실행
print("get_circumference(10):", get_circumference(10))
print("get_circle_area(10):", get_circle_area(10))
2. 패키지 만들기
variable_a = "a 모듈의 변수"
variable_b = "b 모듈의 변수"
import test_pakage.module_a as a
import test_pakage.module_b as b
print(a.variable_a) # a모듈의 변수
print(a.variable_b) # b모듈의 변수
3. __init__.py 파일
패키지를 읽을 때 어떤 처리를 수행해야 하거나 패키지 내부의 모듈을 한꺼번에 가져오고싶을 때 사용
__all__ = ["module_a", "module_b"]
print("테스트 패키지 읽어오기완료")
from test_pakage import * #내부 모듈 모두 읽어오기
print(module_a.variable_a)
print(module_b.variable_b)
728x90