Skip to content

Instantly share code, notes, and snippets.

View metaperl's full-sized avatar

Structure and Interpretation of Computer Programs metaperl

View GitHub Profile
hrldcpr /
Last active June 8, 2024 18:11
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

class _ExceptionHandlerBase:
def __init__(self, handler_map):
self._handler_map = handler_map
def __call__(self, func):
def wrapper(*args, **kwargs):
with self:
func(*args, **kwargs)
return wrapper