고래밥 이야기
Python_module 본문
다음 사이트를 참고하였습니다.
6. Modules — Python 3.11.0 documentation
05-2 모듈 - 점프 투 파이썬 (wikidocs.net)
05-2 모듈
모듈이란 함수나 변수 또는 클래스를 모아 놓은 파일이다. 모듈은 다른 파이썬 프로그램에서 불러와 사용할 수 있게끔 만든 파이썬 파일이라고도 할 수 있다. 우리는 파이썬으로 프로그…
wikidocs.net
6. Modules — Python 3.11.0 documentation
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input
docs.python.org
Script :
As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program. (함수나 변수를 새로 코딩을 할 때마다 불러오는 것은 번거로운 일이다. 따라서 이미 만들어진 파일이 존재하는데, 이것을 script라고 한다.)
Module :
Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module. Such a file is called a module; definitions from a module can be imported into other modules or into the main module. A module is a file containing Python definitions and statements. (그냥,, Script ⊃ Module 이라 생각하면 쉬울 것 같다. 함수나 변수 또는 클래스를 모아 놓은 파일이라고 생각하면 쉬울 것 같다.) 정확한 이해를 위해 다음 예시를 살펴보자.
Package :
A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with a __path__ attribute. (참고로 Module도 여러 Model로 이루어져 있고, 이러한 모음들을 Package라고 한다. 그냥,, Script ⊃ Package ⊃ Module 이라 생각하면 쉬울 것 같다.)
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result
이러한 파일(module) 이름을 fibo.py라고 저장할 수 있으며, 'import'를 이용하여, 파일(module)을 불러올 수 있다. 활용 방안은 다음과 같다. 참고로, 파이썬 확장자 .py로 만든 파이썬 파일은 모두 모듈이다.
import fibo
fibo.fib(1000)
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
fibo.fib2(100)
>>> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
fibo.__name__ # global variable을 뜻한다. 조금 더 자세한 내용은, class에서 다룰 예정이다.
>>> 'fibo'
fib = fibo.fib
fib(500)
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
from fibo import fib, fib2
fib(500)
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
from fibo import * # 모든 함수나 변수들을 불러오겠다는 말이다.
fib(500)
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
import fibo as fib
fib.fib(500) # 앞의 fib는 모듈을, 그 뒤의 fib는 fibo 안에 들어있던 함수를 가르킨다.
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
from fibo import fib as fibonacci # fibo라는 모듈에서 그 안에 속한 fib라는 함수를 불러오는데 이 이름을 fibonacci로 하겠다는 말이다.
fibonacci(500)
>>> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
if __name__ == "main": 의 의미
이 파일을 직접 실행하였을 때는 __name__ == "main" 이 참이되지만, import와 같이 모듈을 불러올 때는 해당 논리식이 거짓이 된다. 참고로, mod1.py를 직접 실행한 경우 __name__은 "main"이 되지만, import한 경우 __name__은 'mod1'이 된다.
'잡다 > 잡다 그 잡채' 카테고리의 다른 글
Python_class (0) | 2022.11.09 |
---|---|
Python_def (0) | 2022.11.09 |
Python_inner product (1) | 2022.11.09 |
Python_The import system (0) | 2022.11.09 |
python_%timeit (0) | 2022.10.31 |