Goed gedaan!

Functies schrijven in Python

Shayne Miel

Software Architect @ Duo Security

Hoofdstuk 1 - Best practices

  • Docstrings
  • DRY en Do One Thing
  • Pass-by-assignment (mutable vs immutable)
Functies schrijven in Python

Hoofdstuk 2 - Contextmanagers

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

Hoofdstuk 3 - Decorators

@my_decorator
def my_decorated_function():
  # do something
def my_decorator(func):
  def wrapper(*args, **kwargs):
    return func(*args, **kwargs)
  return wrapper
Functies schrijven in Python

Hoofdstuk 4 - Meer over decorators

def my_decorator(func):
  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    return func(*args, **kwargs)
  return wrapper
Functies schrijven in Python

Hoofdstuk 4 - Meer over 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
Functies schrijven in Python

Bedankt!

Functies schrijven in Python

Preparing Video For Download...