Skip to content

Instantly share code, notes, and snippets.

@xflr6
Last active June 4, 2022 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xflr6/32ba3f01f56182525193791206c49565 to your computer and use it in GitHub Desktop.
Save xflr6/32ba3f01f56182525193791206c49565 to your computer and use it in GitHub Desktop.
Decorator with an optional parameter
"""Decorator with an optional parameter."""
from collections.abc import Callable
import functools
from typing import Optional
FUNCS = {}
def register(func: Optional[Callable] = None,
name: Optional[str] = None) -> Callable:
if func is None:
return functools.partial(register, name=name)
if name is None:
name = func.__name__
FUNCS[name] = func
return func
@register
def spam():
pass
@register(name='eggs')
def _eggs():
pass
assert FUNCS == {'spam': spam, 'eggs': _eggs}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment