์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

ํŒŒ์ด์ฌ (python) - ๋ชจ๋“ˆ , ํŒจํ‚ค์ง€ ( module , package ) ์ƒ์„ฑ , import์™€ alias

  • -

*ํŒจํ‚ค์ง€ package

๊ธฐ๋Šฅ๋“ค์˜ ๋ฌถ์Œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํŒŒ์ด์ฌ์„ ์ฒ˜์Œ ๋ฐฐ์šธ๋•Œ ์‚ฌ์šฉํ•˜๋Š” print, reverse , range ๋„ ํŒŒ์ด์ฌ์ด ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€์˜ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. (built-in ํ•จ์ˆ˜)

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํด๋” ๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€ ์•ˆ์—๋Š” __init__.py๋ผ๋Š” ํŒŒ์ผ์ด ํฌํ•จ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

(์ด๋ถ€๋ถ„์€ ํŒŒ์ด์ฌ 2.xxxx ๋ฒ„์ ผ์ด ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. 3.xxx๋ฒ„์ ผ์€ ์ด ํŒŒ์ผ์„ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•„๋„ ํŒจํ‚ค์ง€๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.)

*๋ชจ๋“ˆ module

ํŒจํ‚ค์ง€ ์†์˜ ํ•˜๋‚˜ํ•˜๋‚˜์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํด๋” ์†์˜ xxxx.py ํŒŒ์ผ๋“ค ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

*ํŒจํ‚ค์ง€ ๋ฐ ๋ชจ๋“ˆ ์ƒ์„ฑํ•˜๊ธฐ. 

๋กœ๋˜๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘์•„์˜ค๊ณ , ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํŒจํ‚ค์ง€์ธ ํด๋”๋ฅผ ๋จผ์ € ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค. 

๊ฐ€์žฅ ์ƒ์œ„ ํด๋”๊ฐ€ ๋  ํŒจํ‚ค์ง€

ํŒจํ‚ค์ง€๋ช…์€ lotto๊ฐ€ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด ํด๋” ์•ˆ์— ๋ชจ๋“ˆํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

1. __pycache__   : ํด๋ž˜์Šค ์ƒ์„ฑ์‹œ์— ํŒŒ์ด์ฌ์—์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ. (๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋งŒ๋“ค ํ•„์š”X)

2. __init__ : ํŒŒ์ด์ฌ 2.xx๋ฒ„์ ผ์˜ ํ˜ธํ™˜์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ค€๋‹ค. ๋‚ด์šฉ์€ ์—†์–ด๋„ ๋œ๋‹ค. ์ด๋ฆ„์€ ๊ณตํ†ต __init__.py

3.make_number :

from random import randint

class MakeNum():

    def makenum():
        s = []
        for i in range(7) :      #7๋ฒˆ ๋ฐ˜๋ณต
            a = randint(1,50)    #๋กœ๋˜๋Š” 1 ~ 50๋ฒˆ
            s.append(a)          #์ƒ์„ฑ๋œ ๋ฒˆํ˜ธ s์— ์ €์žฅ
        print(s)
    

4. probability.py

def proba () :
    a = input("๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜")
    print(int(a)*0.0000122774,"%์ž…๋‹ˆ๋‹ค.")

 

*ํŒจํ‚ค์ง€ ๋ฐ ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ

์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด ํŒจํ‚ค์ง€๊ฐ€ lottoํŒจํ‚ค์ง€๊ฐ€ ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ lottoํด๋” ๋ฐ–์—์„œ ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์•„๊นŒ ๋งŒ๋“  3๋ฒˆํŒŒ์ผ (๋ฒˆํ˜ธ์ƒ์„ฑ ํŒŒ์ผ)์€ Class๊ฐ€ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. 

#ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์ž„ํฌํŠธ ๋ฐฉ์‹

# 1 (ํด๋ž˜์Šค import)

from lotto.make_number import MakeNum
MakeNum.makenum()

# 2 (ํด๋ž˜์Šค import)

from lotto.make_number import *   # *๋Š” ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ž„ํฌํŠธ ํ•œ๋‹ค๋Š” ๋œป.
MakeNum.makenum()

# 3 (ํด๋ž˜์Šค import)

from lotto.make_number import MakeNum as MN    # MakeNum์„ MN์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฒ ๋‹ค๋Š” ๋œป.
MN.makenum()

( 2๋ฒˆ์˜ import* ์˜ ๊ฒฝ์šฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•˜์—ฌ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ์ข‹์ง€ ์•Š๋‹ค๊ณ ํ•ฉ๋‹ˆ๋‹ค)

#ํ•จ์ˆ˜ ์ž„ํฌํŠธ


#๋ฐฉ๋ฒ• 1
from lotto.probability import proba 
proba()

#๋ฐฉ๋ฒ• 2
import lotto.probability as t
t.proba()

#๋ฐฉ๋ฒ• 3
import lotto.probability
lotto.probability.proba()
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.