Great job!

Writing Functions in Python

Shayne Miel

Software Architect @ Duo Security

Chapter 1 - Best Practices

  • Docstrings
  • DRY and Do One Thing
  • Pass by assignment (mutable vs immutable)
Writing Functions in Python

Chapter 2 - Context Managers

with my_context_manager() as value:
  # do something
@contextlib.contextmanager
def my_function():
  # this function can be used in a "with" statement now
Writing Functions in Python

Chapter 3 - Decorators

@my_decorator
def my_decorated_function():
  # do something
def my_decorator(func):
  def wrapper(*ars, **kwargs):
    return func(*args, **kwargs)
  return wrapper
Writing Functions in Python

Chapter 4 - More on Decorators

def my_decorator(func):
  @functools.wraps(func)
  def wrapper(*ars, **kwargs):
    return func(*args, **kwargs)
  return wrapper
Writing Functions in Python

Chapter 4 - More on Decorators

def decorator_that_takes_args(a, b, c):
  def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
      return func(*args, **kwargs)
    return wrapper
  return decorator
Writing Functions in Python

Thank you!

Writing Functions in Python

Preparing Video For Download...