Skip to content

Commit 81a918e

Browse files
committed
모듈은 끝.
패키지는 맛보기만 했다.
1 parent 97e8283 commit 81a918e

File tree

15 files changed

+147
-0
lines changed

15 files changed

+147
-0
lines changed

20191217.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 점프 투 파이썬 207page ~
2+
3+
# ============================== 모듈 ==============================
4+
5+
import mod1
6+
7+
print(mod1.add(4, 3))
8+
print(mod1.sub(4, 3))
9+
print('=' * 50)
10+
11+
# 모듈 이름 없이 사용하는 방법 1
12+
from mod1 import add
13+
print(add(6, 4))
14+
print('=' * 50)
15+
16+
from mod1 import * # mod1에 있는 모든 함수를 사용하겠다는 의미
17+
print(add(4, 3))
18+
print(sub(4, 3))
19+
print('=' * 50)
20+
21+
# 클래스나 변수 등을 포함한 모듈
22+
23+
import mod2
24+
25+
print(mod2.PI)
26+
a = mod2.Math()
27+
print(a.solv(2))
28+
print(mod2.add(4, 2))
29+
print('=' * 50)
30+
31+
# 다른 디렉토리에 있는 모듈 사용 방법.
32+
33+
# 1. sys.path.append(모듈을 설치한 디렉토리) 사용
34+
35+
# import sys
36+
# print(sys.path) # 여기에 포함된 경로에 있으면 바로 모듈을 불러서 사용 가능하다.
37+
# sys.path.append('./Modules')
38+
# print(sys.path)
39+
# sys.path.pop()
40+
# print(sys.path)
41+
42+
43+
# 2.PYTHONPATH 환경변수 사용
44+
45+
# cmd에서
46+
# set PYTHONPATH = D:\PythonExam\Modules 라고 쓴 후 사용
47+
# set PYTHONPATH = D:\PythonExam\Modules
48+
49+
# ============================== 패키지 ==============================
50+
51+
"""
52+
패키지
53+
도트(.)를 활용해 파이썬의 모듈을 계층적으로 관리할 수 있게 해준다.
54+
55+
"""
56+
57+
# 패키지 내부 함수 실행 1
58+
import game.sound.echo
59+
game.sound.echo.echo_test()
60+
61+
# 패키지 내부 함수 실행 2
62+
from game.sound import echo
63+
echo.echo_test()
64+
65+
# 패키지 내부 함수 실행 3
66+
from game.sound.echo import echo_test
67+
echo_test()
68+
69+
# 불가능한 패키지 내부 함수 실행 방법
70+
import game
71+
game.sound.echo.echo_test() # ??? 왜 됨???
72+
73+
# __init__.py 파일의 용도 .... 부터는 내일...
74+
75+
"""
76+
__init__ 파일은 해당 디렉토리가 패키지의 일부임을 알려주는 용도로 사용한다.
77+
(python3.3부터는 없어도 인식하나 이전 버전과의 호환을 위해 남겨둔다.)
78+
79+
"""

Modules/mod1-1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 20191217.py 에서 사용하는 모듈
2+
def add(a, b):
3+
return a + b
4+
5+
def sub(a, b):
6+
return a - b
7+
8+
# print(add(1, 4))
9+
# print(sub(4, 2))
10+
# 위 처럼 만 사용하면 다른 모듈에서 import 할 때 print의 결과가 출력된다.
11+
# 이를 방지하려면 아래와 같이 바꾼다.
12+
13+
if __name__ == '__main__': # 직접 mod1.py를 실행 했을때만 참이 되어 실행된다.
14+
print(add(1, 4))
15+
print(sub(4, 2))
16+
17+
# __name__ 란?
18+
# 파이썬 내부에서 사용하는 특수한 변수명이다.
19+
# 직접 현재 파일을 실행할 경우에는 '__main__' 이라는 값이 들어가지만
20+
# 다른 모듈에서 import 할 경우에는 확장자를 제외한 모듈의 이름 값이 저장 된다.

Modules/mod2-1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 20191217.py 에서 사용하는 모듈2
2+
3+
PI = 3.141592
4+
5+
class Math:
6+
def solv(self, r):
7+
return PI * (r ** 2)
8+
9+
def add(a, b):
10+
return a + b

__pycache__/mod1.cpython-38.pyc

400 Bytes
Binary file not shown.

__pycache__/mod2.cpython-38.pyc

506 Bytes
Binary file not shown.

game/__init__.py

Whitespace-only changes.
121 Bytes
Binary file not shown.

game/graphic/__init__.py

Whitespace-only changes.

game/graphic/render.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 20191217.py 패키지 실습을 위한 모듈
2+
3+
def render_test():
4+
print('render')

game/sound/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)