Created
May 20, 2020 21:27
-
-
Save maxpoletaev/4e3ca200eb5b663e901cc8b1263b8566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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