본문 바로가기
lang/python

[decorator]

by Wordbe 2019. 7. 25.
728x90

decorator


함수 전에 붙여서 클래스의 메소드임을 명시합니다.

class Pizza(object):
    def __init__(self):
        self.toppings = []

    def __call__(self, topping):
        # When using '@instance_of_pizza' before a function definition
        # the function gets passed onto 'topping'.
        self.toppings.append(topping())

    def __repr__(self):
        return str(self.toppings)

pizza = Pizza()

@pizza
def cheese():
    return 'cheese'
@pizza
def sauce():
    return 'sauce'

print pizza
# ['cheese', 'sauce']
728x90

'lang > python' 카테고리의 다른 글

[mac] jupyter, virtualenv, python, pip 설치  (0) 2020.09.26

댓글