Skip to content

Instantly share code, notes, and snippets.

@sjkillen
Created March 4, 2023 00:35
Show Gist options
  • Save sjkillen/3f04ea89103e82dfebdc8dd98cc382d6 to your computer and use it in GitHub Desktop.
Save sjkillen/3f04ea89103e82dfebdc8dd98cc382d6 to your computer and use it in GitHub Desktop.
from functools import singledispatch, wraps
from collections.abc import Iterable
def one_or_many(fn):
@wraps(fn)
@singledispatch
def wrapper(*args, **kwargs):
fn(*args, **kwargs)
@wrapper.register
def _(items: Iterable, *args, **kwargs):
for item in items:
wrapper(item, *args, **kwargs)
return wrapper
@one_or_many
def print_fancy(item):
print(f">>>>>>>>{item}<<<<<<<<")
print_fancy(1)
print_fancy([2, 3, 4])
print_fancy([[[[[[[[[5, 6, 7, 8]]]]]]]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment