์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/40
[python ๊ธฐ์ด] 8. ํด๋์ค , ์์ , ๋ค์ค์์
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/39 https://silvercoding.tistory.com/38 https://silvercoding.tistory.com/37 https://silvercoding.tistory.com/36..
silvercoding.tistory.com
* ํด๋ - ํจํค์ง, ํ์ผ - ๋ชจ๋ ์ด๋ผ๊ณ ํ ์ ์๋ค.
* ์๋ ๊ฒฝ๋ก - . : ๋ถ๋ชจ ๋๋ ํ ๋ฆฌ / .. : ํ์ฌ ๋๋ ํ ๋ฆฌ
์ฐ์ ์ค๋ ์ค์ต์ ์ํด์
pkg ํด๋๋ฅผ ์์ฑํ๊ณ , ๊ทธ ์์ ํ์ผ์ ๋ง๋ค์ด ์ค๋ค.
(1) __init__.py
* ์ฉ๋ : ํด๋น ๋๋ ํ ๋ฆฌ๊ฐ ํจํค์ง์์ ์ ์ธ
* Python 3.x : ํ์ผ์ด ์์ด๋ ํจํค์ง๋ฅผ ์ธ์ ํ์ง๋ง , ํ์ ํธํ์ ์ํด ์์ฑํด ๋๋ ๊ฒ์ ์ถ์ฒํ๋ค.
(2) fibonacci.py
class Fibonacci:
def __init__(self, title='fibonacci'):
self.title = title
def fib(n): # ํผ๋ณด๋์น ์ถ๋ ฅ
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()
def fib2(n): # ํผ๋ณด๋์น ๋ฆฌ์คํธ ๋ฆฌํด
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a + b
return result
fib๋ ํผ๋ณด๋์น ์์ด์ ์ถ๋ ฅ, fib2๋ ๋ฆฌ์คํธ๋ก ์ถ๋ ฅํด ์ฃผ๋ ํจ์์ด๋ค.
(3) calculations.py
def add(l, r):
return l + r
def mul(l, r):
return l * r
def div(l, r):
return l / r
๋ํ๊ธฐ, ๊ณฑํ๊ธฐ, ๋๋๊ธฐ ๋จ์ ๊ณ์ฐ ํจ์๋ค.
(4) prints.py
def prt1():
print("I'm NiceGirl!")
def prt2():
print("I'm GoodGirl!")
# ๋จ์ ์คํ (๋
๋ฆฝ์ ์ผ๋ก ํ์ผ ์คํ) : ํจ์๊ฐ ์ ๋ง๋ค์ด ์ก๋์ง ํ์ธํ๊ธฐ ์ํด ์ด ํ์ผ์์๋ง ์คํํ๋๋ก ์ค์
if __name__ == "main":
prt1()
prt2()
๋จ์ ์ถ๋ ฅ๋ฌธ์ด๊ณ , ๋จ์์คํ์ผ๋ก ๋ค๋ฅธ ํ์ผ์์ ์ด ๋ชจ๋์ ์คํํ์ ๋ ์ถ๋ ฅ๋์ง ์๊ณ , ํ์ฌ ํ์ผ์์๋ง ์คํ์ด ๋๋๋ก ํ๋ if๋ฌธ์ ์ ์ธํ๋ค.
- ์์ 1 : ํด๋์ค
from pkg.fibonacci import Fibonacci
์์ ๊ฐ์ด ํด๋์ค๋ฅผ importํด ์ฌ ์ ์๋ค.
Fibonacci.fib(300)
0 1 1 2 3 5 8 13 21 34 55 89 144 233
์ธ์คํด์ค ๋ฉ์๋๊ฐ ์๋ ํด๋์ค ๋ฉ์๋์์ผ๋ฏ๋ก ํด๋์ค ์์ฒด๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
print("ex1 : ", Fibonacci.fib2(400))
print('ex1 : ', Fibonacci().title) # ์ธ์คํด์คํ ์์ผ์ผ ํจ.
ex1 : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
ex1 : fibonacci
title์ ์ธ์คํด์ค ๋ณ์๊ธฐ ๋๋ฌธ์ ์ธ์คํด์คํ ์์ผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
- ์์ 2 : ํด๋์ค 2
from pkg.fibonacci import *
๋ชจ๋์ ๋ชจ๋ ํด๋์ค๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด๋ค. ์ด ๋ฐฉ๋ฒ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ง์ด ์ฐจ์งํ๋ฏ๋ก ๊ถ์ฅ๋์ง ์๋๋ค. ํ์ํ ๊ฒ๋ง ๋ถ๋ฌ์ค๋ ๊ฒ์ด ํจ์จ์ ์ด๋ค.
Fibonacci.fib(500)
print("ex2 : ", Fibonacci.fib2(600))
print('ex2 : ', Fibonacci().title) # ์ธ์คํด์คํ ์์ผ์ผ ํจ.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
ex2 : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
ex2 : fibonacci
- ์์ 3 : ํด๋์ค - ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ
from pkg.fibonacci import Fibonacci as fb
์ด์ ๊ฐ์ด ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ง์ด ๋ณด์์ ๊ฒ์ด๋ค.
fb.fib(1000)
print("ex3 : ", fb.fib2(1600))
print('ex3 : ', fb().title)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
ex3 : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
ex3 : fibonacci
- ์์ 4 : ํจ์
import pkg.calculations as c
print("ex4 : ", c.add(10, 100))
print("ex4 : ", c.mul(10, 100))
ex4 : 110
ex4 : 1000
- ์์ 5 : ํจ์ - ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ (ํ์ํ ๋งํผ๋ง ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ ์ต๊ด์ด๋ค. )
from pkg.calculations import div as d
print('ex5 : ', int(d(100, 10)))
ex5 : 10
์ด๋ ๊ฒ ํ์ํ ํจ์๋ง ๋ถ๋ฌ์์ ์ฌ์ฉํ ์ ์๋ค.
- ์์ 6
import pkg.prints as p
import builtins # ํ์ด์ฌ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ํจ์๋ค
p.prt1()
p.prt2()
print()
print(dir(builtins))
I'm NiceGirl!
I'm GoodGirl!
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
builtins์๋ ํ์ด์ฌ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ํจ์๋ค์ธ๋ฐ, prints.py์์ ์ฌ์ฉํ๋ __name__์ด ๋ค์ด๊ฐ ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 11. ์์ธ ์ข ๋ฅ์ ์ฒ๋ฆฌ , try , except , else, raise (0) | 2021.08.02 |
---|---|
[python ๊ธฐ์ด] 10. ํ์ผ ์ฝ๊ธฐ , ์ฐ๊ธฐ (open) (0) | 2021.08.02 |
[python ๊ธฐ์ด] 8. ํด๋์ค , ์์ , ๋ค์ค์์ (0) | 2021.08.01 |
[python ๊ธฐ์ด] 7. ํจ์ ๋ฐ ๋๋ค (lambda) (0) | 2021.07.31 |
[python ๊ธฐ์ด] 6. ํ์ด์ฌ ํ๋ฆ์ ์ด (๋ฐ๋ณต๋ฌธ) - for, while (0) | 2021.07.31 |