3.4. Function Decorator with Methods

3.4.1. Syntax

  • mydecorator is a decorator name

  • method is a method name

  • instance is an instance

  • args arbitrary number of positional arguments

  • kwargs arbitrary number of keyword arguments

Syntax:
class MyClass:
    @mydecorator
    def my_method(self, *args, **kwargs):
        pass


obj = MyClass()
obj.my_method()
Is equivalent to:
class MyClass:
    def my_method(self, *args, **kwargs):
        pass


obj = MyClass()
obj.my_method = mydecorator(obj.my_method)

3.4.2. Definition

  • decorator is a decorator name

  • method is a method name

  • instance is an instance

  • args arbitrary number of positional arguments

  • kwargs arbitrary number of keyword arguments

def decorator(method):
    def wrapper(instance, *args, **kwargs):
        return method(instance, *args, **kwargs)
    return wrapper

3.4.3. Usage

def if_allowed(method):
    def wrapper(instance, *args, **kwargs):
        if instance._is_allowed:
            return method(instance, *args, **kwargs)
        else:
            print('Sorry, Permission Denied')
    return wrapper


class MyClass:
    def __init__(self):
        self._is_allowed = True

    @if_allowed
    def do_something(self):
        print('Doing...')

    @if_allowed
    def do_something_else(self):
        print('Doing something else...')


my = MyClass()

my.do_something()           # Doing...
my.do_something_else()      # Doing something else...

my._is_allowed = False

my.do_something()           # Sorry, you cannot do anything
my.do_something_else()      # Sorry, you cannot do anything

3.4.4. Examples

def make_paragraph(method):

    def wrapper(instance, *args, **kwargs):
        value = method(instance, *args, **kwargs)
        print(f'<p>{value}</p>')
        return value

    return wrapper


class HTMLReport:

    @make_paragraph
    def first_method(self, *args, **kwargs):
        return 'First Method'

    @make_paragraph
    def second_method(self, *args, **kwargs):
        return 'Second Method'


if __name__ == "__main__":
    x = HTMLReport()
    x.first_method()
    x.second_method()


# <p>First Method</p>
# <p>Second Method</p>

3.4.5. Assignments

3.4.5.1. Decorator Methods Alive

English
  1. Use data from "Input" section (see below)

  2. Create if_alive method decorator

  3. Decorator will allow running make_damage method only if current_health is greater than 0

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj kodu z sekcji "Input" (patrz poniżej)

  2. Stwórz dekorator metod if_alive

  3. Dekotrator pozwoli na wykonanie metody make_damage, tylko gdy current_health jest większe niż 0

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
class Hero:
    def __init__(self, name):
        self.name = name
        self.current_health = 100

    @if_alive
    def make_damage(self):
        return 10


hero = Hero('Jan Twardowski')
print(hero.make_damage())
# 10

hero.current_health = -10
print(hero.make_damage())
# Traceback (most recent call last):
#     ...
# RuntimeError: Hero is dead and cannot make damage