This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Decorators using the @ symbol ########## | |
def double_args(func): | |
def wrapper(a, b): | |
return func(a * 2, b * 2) # Modify input arguments before passing to requested function | |
return wrapper | |
@double_args # This is to indicate the decorators is used on the multiple function | |
def multiple(a, b): | |
return a * b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Memoize example which stores results for fast look ups | |
import time | |
def memoize(func): | |
cache = {} | |
def wrapper(*args, **kwargs): | |
if (args, kwargs) not in cache: | |
cache[(args, kwargs)] = func(*args, **kwargs) | |
return cache[(args, kwargs)] |