Skip to content

Instantly share code, notes, and snippets.

@victor-iyi
Created April 10, 2018 11:05
Show Gist options
  • Select an option

  • Save victor-iyi/0dc50339e0cea1a8e1aded0816d11265 to your computer and use it in GitHub Desktop.

Select an option

Save victor-iyi/0dc50339e0cea1a8e1aded0816d11265 to your computer and use it in GitHub Desktop.
An example showing off function decorators & class decorators
"""
@author Victor I. Afolabi
A.I. Engineer & Software developer
javafolabi@gmail.com
Created on 05 October, 2017 @ 03:35 PM.
Copyright (c) 2017. Victor. All rights reserved.
"""
print('1. METHOD DECORATORS. [NO ARGUMENTS]')
def meth_decorator(original_func):
# Inner/wrapper function.
def wrap():
print('\nMethod decorator ran before {}()'.format(original_func.__name__))
# Return the call to the original function.
return original_func()
# Return the inner function waiting to be called.
return wrap
# Method to be wrapped.
print('Method Decorator Syntax 1:')
def display():
print('Hello, World!')
# Step 1: Wrap the display method.
display = meth_decorator(display)
# Step 2: Call the wrapped display method.
display()
print(70 * '=')
print('Method Decorator Syntax 2:')
@meth_decorator
def display():
print('Hello, World!')
# Call the wrapped display method.
display()
print(80 * '#', '\n')
################################################################################
print('2. METHOD DECORATORS. [WITH ARGUMENTS]')
def meth_decorator(original_func):
# Inner/wrapper function (optional argument & keyword arguments).
def wrap(*args, **kwargs):
msg = '\nMethod decorator ran before {}({}, {})'\
.format(original_func.__name__, args, kwargs)
print(msg)
# Return the original function passing the args and kwargs.
return original_func(*args, **kwargs)
# Return the wrapper function waiting to be called.
return wrap
# Method to be wrapped.
print('Method Decorator Syntax: 1')
# Method without argument.
def display():
print('Hello, World!')
# Method with argument.
def display_args(name, greet='Hello'):
print('{}, {}!'.format(name, greet))
# Step 1: Wrap the display method.
display = meth_decorator(display)
display_args = meth_decorator(display_args)
# Step 2: Call the wrapped display method.
display()
display_args('John')
display_args('Jane', greet='Hi')
print(70 * '=')
print('Method Decorator Syntax 2:')
# Method without argument.
@meth_decorator
def display():
print('Hello, World!')
# Method with argument.
@meth_decorator
def display_args(name, greet='Hello'):
print('{}, {}!'.format(name, greet))
# Call the wrapped methods.
display()
display_args('John')
display_args('Jane', greet='Hi')
print(80 * '#', '\n')
###############################################################################
print('1. CLASS DECORATORS.')
class decorator(object):
def __init__(self, original_func):
self.original_func = original_func
def __call__(self, *args, **kwargs):
msg = '\nClass decorator ran before {}({}, {})'\
.format(self.original_func.__name__, args, kwargs)
print(msg)
return self.original_func(*args, **kwargs)
@decorator
def display():
print('Hello, World!')
@decorator
def display_args(name, greet='Hello'):
print('{}, {}!'.format(name, greet))
display()
display_args('Jimmy')
display_args('Doe', "What's poppin'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment