Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Created May 20, 2020 21:27
Show Gist options
  • Save maxpoletaev/4e3ca200eb5b663e901cc8b1263b8566 to your computer and use it in GitHub Desktop.
Save maxpoletaev/4e3ca200eb5b663e901cc8b1263b8566 to your computer and use it in GitHub Desktop.
from functools import wraps
import inspect
def singleton(func):
@wraps(func)
def decorator():
if not hasattr(func, "instance"):
func.instance = func()
return func.instance
@wraps(func)
async def async_decorator():
if not hasattr(func, "instance"):
func.instance = await func()
return func.instance
if inspect.iscoroutinefunction(func):
return async_decorator
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment