Coding/Python

[Python] Module

๊น€์œ ๋‹ˆ์ฝ˜ 2022. 3. 7. 17:32

๋ชจ๋“ˆ(๋ฌถ์Œ)์ด๋ž€?

  1. ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋“ค์„ ๋ชจ์•„ ๋†“์€ library๋“ค ์˜๋ฏธ
       ํšŒ์‚ฌ, ๊ทธ๋ฃน, ๋ชจ๋ฅด๋Š” ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์™ธ๋ถ€ library๋“ค ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•, ์™ธ๋ถ€์— ๋‚ด ์ฝ”๋“œ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

๋ชจ๋“ˆ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
2-1. ๋ชจ๋“ˆ์˜ ํŒŒ์ผ ํ™•์žฅ์ž : py
2-2. ๋ชจ๋“ˆ ์‚ฌ์šฉ ๋ฌธ๋ฒ•
    a. import file๋ช…
    b. from file๋ช… import ํ•จ์ˆ˜๋ช…
    c. import file๋ช… as ๋ณ„์นญ

 
 
 
%%writefile mymath.py  #mymath.py๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅ

mypi = 3.14
def add(a,b):
    return a+b

def area(r):
    return r*r*mypi

 

 

mymath ๋ชจ๋“ˆ์˜ ๊ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ๋ฐ ์ถœ๋ ฅ

# from file๋ช… import ํ•จ์ˆ˜๋ช…1 [, ํ•จ์ˆ˜๋ช…2]
import mymath as m

print(m.add(1,2))
print(m.area(3))

or

from mymath import add
from mymath import area

add(1,4)
area(3)