A Python decorator allows you to execute some code before and after the wrapped function is executed.

We typically use decorators for validation, logging, introspection or whenever we need to extend the functionality of an existing function without modifying it directly.

Basic Setup for a decorator

# py:3
def logger(func):

    def wrap(*args, **kwards):
        if "House" in args:
            print "House Logging"
        else:
            print "logging"
        return func(*args, **kwards)
    return wrap

@logger
def say_hello(word):
    print word
say_hello("House")
>> House Logging
>> House

January 1, 2018


Previous post
Save terminal output to a file All output will be sent to terminal and file. Append to existing file Source: Stackoverflow
Next post
Find the difference between two dictionaries/json Use Python to find the difference between two dictionaries with nested dictionaries