Python

Python Menu

Decorators allow you to make simple modifications to callable objects like functions, methods, or classes. We shall deal with functions for this tutorial. The syntax:

@decorator
def functions(arg):
    return "value"

Is equivalent to:

def function(arg):
    return "value"
function = decorator(function) # this passes the function to the decorator, and reassigns it to the functions

A decorator is just another function which takes a functions and returns one, thus:

def repeater(old_function):
    def new_function(*args, **kwds):
        # runs the old function
        old_function(*args, **kwds)

        # does it twice
        old_function(*args, **kwds)

    # return the new_function, or it wouldn't reassign it to the value
    return new_function

Example:

def multiply(multiplier): def multiply_generator(old_function): def new_function(*args, **kwds): return multiplier * old_function(*args, **kwds) return new_function return multiply_generator # it returns the new generator
# Usage @multiply(3) # multiply is not a generator, but multiply(3) is def return_num(num): return num
# Now return_num is decorated and reassigned into itself return_num(5) # should return 15

Exercise

Make a decorator factory which returns a decorator that decorates functions with one argument. The factory should take one argument, a type, and then returns a decorator that makes function should check if the input is the correct type. If it is wrong, it should print("Bad Type") (In reality, it should raise an error, but error raising isn't in this tutorial). Look at the tutorial code and expected output to see what it is if you are confused (I know I would be.) Using isinstance(object, type_of_object) or type(object) might help.

def type_check(correct_type): # put code here
@type_check(int) def times2(num): return num*2
print(times2(2)) times2('Not A Number')
@type_check(str) def first_letter(word): return word[0]
print(first_letter('Hello World')) first_letter(['Not', 'A', 'String'])
def type_check(correct_type): def check(old_function): def new_function(arg): if (isinstance(arg, correct_type)): return old_function(arg) else: print("Bad Type") return new_function return check
@type_check(int) def times2(num): return num*2
print(times2(2)) times2('Not A Number')
@type_check(str) def first_letter(word): return word[0]
print(first_letter('Hello World')) first_letter(['Not', 'A', 'String'])
test_output_contains("4") test_output_contains("Bad Type") test_output_contains("H") test_output_contains("Bad Type") success_msg("Excellent!")

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods