by peachy_no_pie · 4y ago · 0 comments
def foo(Bar=Bar):
bar = Bar()
...
Pass in the class or any callable and then call it in the body to make an instance.This is commonly used to implement logging callbacks e.g.
from collections.abc import Callable
def log_fn(message: str = ''):
print(f'This is your log: {message}')
def run_read_file(file_to_read:str, log_handler: Callable=log_fn):
contents = open(file_to_read).readlines()
for line in contents:
log_handler(message=line)