Jayden1116 2022. 1. 26. 00:35

λ°μ½”λ ˆμ΄ν„°(@)

μ–΄μƒ‰ν•˜κ³  반볡적인 ν•¨μˆ˜μ˜ ν‘œν˜„μ„ 쀄이기 μœ„ν•΄ μ œμ•ˆ
ν•¨μˆ˜ 외에도 클래슀, μ œλ„ˆλ ˆμ΄ν„° λ“± λ‹€μ–‘ν•œ νƒ€μž…μ—μ„œλ„ μ‚¬μš©
λ°μ½”λ ˆμ΄ν„° -> 말 κ·ΈλŒ€λ‘œ ν•¨μˆ˜λ₯Ό 'κΎΈλ©°μ£ΌλŠ”' ν˜Ήμ€ 'μž₯μ‹ν•˜λŠ”', '포μž₯ν•˜λŠ”' κΈ°λŠ₯

μ°Έκ³ :pep318

ν•¨μˆ˜μ˜ λ°μ½”λ ˆμ΄ν„°(@)

μ˜ˆμ‹œλΆ€ν„° λ³΄κ² μŠ΅λ‹ˆλ‹€.

def one():
    print("Hello")
    print("oneμ΄λΌλŠ” ν•¨μˆ˜")

def two():
    print("Hello")
    print("twoλΌλŠ” ν•¨μˆ˜")

def three():
    print("Hello")
    print("threeλΌλŠ” ν•¨μˆ˜")

μœ„μ˜ μ˜ˆμ‹œλŠ” μ•„μ£Ό κ°„λ‹¨ν•˜κΈ°μ— 감이 μ•ˆμ˜¬ 수 μžˆμ§€λ§Œ, κ³΅ν†΅μ μœΌλ‘œ λ“€μ–΄κ°„ print("Hello")λ₯Ό ν•œλ²ˆμ— λ¬Άμ–΄μ„œ μ“Έ 수 μžˆμŠ΅λ‹ˆλ‹€.

def Hello_deco(func): # funcλŠ” μ‹€ν–‰ν•  ν•¨μˆ˜
    def Hello(): # μ‹€ν–‰ν•  ν•¨μˆ˜λ₯Ό κ°μ‹ΈλŠ”(wrap) ν•¨μˆ˜
        print("Hello")
        func()
    return Hello # Hello ν•¨μˆ˜λ₯Ό 리턴

@Hello_deco
def one():
    print("oneμ΄λΌλŠ” ν•¨μˆ˜")

@Hello_deco
def two():
    print("twoλΌλŠ” ν•¨μˆ˜")

@Hello_deco
def three():
    print("threeλΌλŠ” ν•¨μˆ˜")

μœ„μ™€ 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (μ§€κΈˆμ€ μ§§μ§€λ§Œ, μ½”λ“œκ°€ 훨씬 κΈΈμ–΄μ‘Œλ‹€κ³  κ°€μ •ν•˜λ©΄ λ°μ½”λ ˆμ΄ν„°μ˜ νŽΈλ¦¬ν•¨μ„ λŠλ‚„ 수 μžˆμŠ΅λ‹ˆλ‹€!)

Hello_deco(one())

# Hello
# oneμ΄λΌλŠ” ν•¨μˆ˜

λ°μ½”λ ˆμ΄ν„° μ‚¬μš©ν•˜λŠ” 이유

DRY(Don't Repeat Yourself)

파이썬 κ°œλ°œμžλ“€μ΄ μ§€ν‚€λ €ν•˜λŠ” 원칙 쀑 μ½”λ“œμ˜ 반볡 μ‚¬μš©μ„ 쀄이기 μœ„ν•œ DRY 원칙
이 원칙을 지킀기 μœ„ν•΄ 파이썬 κ°œλ°œμžλ“€μ€ 상속, λ””μŠ€ν¬λ¦½ν„°, μ œλ„ˆλ ˆμ΄ν„° λ“± λ‹€μ–‘ν•œ κΈ°μˆ μ„ μ‚¬μš©
특히 λ°μ½”λ ˆμ΄ν„°λŠ” μ‹œκ°μ μœΌλ‘œ λ‹€λ₯Έ κΈ°μˆ λ“€λ³΄λ‹€ κΉ”λ”ν•˜κ³  κ°„κ²°ν•œ μ½”λ“œλ₯Ό λ§Œλ“€μ–΄μ£ΌκΈ°μ— 많이 μΆ”μ²œλ¨

μ‹€μ œ μ–΄λ–€ ν”„λ‘œκ·Έλž˜λ°μ„ ν•˜λŠ” κ²½μš°κ°€ μ•„λ‹ˆλΌλ©΄ 잘 μ‚¬μš©μ•ˆν•˜λ”λΌλ„ νŒ¨ν‚€μ§€λ₯Ό μ΄μš©ν•  λ•Œ 자주 μ ‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

@property λ°μ½”λ ˆμ΄ν„°

μ˜ˆμ‹œ)

class test:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.full = first + ' ' + last

name = test('Jay', 'Lee')

print(name.first) #-> 'Jay' 좜λ ₯
print(name.last) #-> 'Lee' 좜λ ₯
print(name.full) #-> 'Jay Lee' 좜λ ₯
name.last = 'Den'
print(name.last) #-> 'Den' 좜λ ₯
print(name.full) #-> 'Jay Lee' 좜λ ₯

μœ„μ˜ 경우, name.lastλ₯Ό 바꿔주어도 이미 classλ₯Ό μ„ μ–Έν•  λ•Œ, Jay와 Leeλ₯Ό λ°›κΈ° λ•Œλ¬Έμ— name.full을 ν”„λ¦°νŠΈν•΄λ„ Den이 반영된 값이 μ•„λ‹Œ, κ·ΈλŒ€λ‘œ Jay Leeκ°€ 좜λ ₯λ©λ‹ˆλ‹€. 이λ₯Ό λ°˜μ˜ν•΄μ£ΌκΈ° μœ„ν•΄ μ•„λž˜μ™€ 같이 μ½”λ“œλ₯Ό λ³€κ²½ν•©λ‹ˆλ‹€.

class test_1:
    def __init__(self, first, last):
        self.first = first
        self.last = last

    def full(self):
        return self.first + ' ' + self.last

name = test_1('Jay', 'Lee')

μœ„μ™€ 같이 full()에 λŒ€ν•œ ν•¨μˆ˜λ₯Ό λ”°λ‘œ μ •μ˜ν•˜μ—¬ λ°˜μ˜ν•΄μ€„ 수 μžˆμŠ΅λ‹ˆλ‹€.
λ‹€λ§Œ, 이 λ•ŒλŠ” name.full()둜 ν˜ΈμΆœν•˜μ—¬ λ©”μ†Œλ“œν˜•μ‹μ„ μ·¨ν•΄μ•Όν•©λ‹ˆλ‹€. 이λ₯Ό νŠΉμ„± ν˜•νƒœλ‘œ λ°”κΎΈκΈ° μœ„ν•΄ property λ°μ½”λ ˆμ΄ν„°κ°€ μ‚¬μš©λ©λ‹ˆλ‹€.

class test_1:
    def __init__(self, first, last):
        self.first = first
        self.last = last

    @property
    def full(self):
        return self.first + ' ' + self.last

name = test_1('Jay', 'Lee')

μœ„μ™€ 같은 κ²½μš°μ—”, name.full둜 ν˜ΈμΆœν•˜λ©΄ 값이 λ¦¬ν„΄λ©λ‹ˆλ‹€.

getter, setter λ°μ½”λ ˆμ΄ν„°

μœ„μ˜ propertyκ°€ getter(κ°€μ Έμ˜¬ λ•Œ μ‚¬μš©ν•˜λŠ” λ°μ½”λ ˆμ΄ν„°)의 역할을 ν–ˆμœΌλ―€λ‘œ setter(값을 μ„€μ •ν•΄μ£ΌλŠ” λ°μ½”λ ˆμ΄ν„°)λ₯Ό μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

class test_1:
    def __init__(self, first, last):
        self.first = first
        self.last = last
    @property
    def full(self):
        return self.first + ' ' + self.last

    @full.setter
    def full(self, new_full):
        first, last = new_full.split()
        self.first = first
        self.last = last

name1 = test_1('Jay', 'Lee')

print(name1.first) #-> 'Jay' 좜λ ₯
print(name1.last) #-> 'Lee' 좜λ ₯
print(name1.full) #-> 'Jay Lee' 좜λ ₯
name1.full = 'Hyo Lee' #-> name1.full에 'Hye Lee'을 μ§€μ •ν•˜κ²Œ 되면
print(name1.first) #-> 'Hyo' 좜λ ₯
print(name1.last) #-> 'Lee' 좜λ ₯

μœ„μ™€ 같이, μ΄λ²ˆμ—” full을 λ³€κ²½ν•˜μ˜€μ„ λ•Œ, first 및 last 값이 λ³€κ²½λ˜λŠ” 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.