the none check could use is instead of =herebut it' trivial test either way) onlynonlocal def singleton(aclass)instance none def oncall(*args**kwargs)nonlocal instance if instance =noneinstance aclass(*args**kwargsreturn instance return oncall on decoration on instance creation and later nonlocal one scope per class in either python or ( and later)you can also code self-contained solution with either function attributes or class instead the first of the following codes the formerleveraging the fact that there will be one oncall function per decoration--the object namespace serves the same role as an enclosing scope the second uses one instance per decorationrather than an enclosing scopefunction objector global table in factthe second relies on the same coding pattern that we will later see is common decorator class blunder--here we want just one instancebut that' not usually the case and xfunc attrsclasses (alternative codingsdef singleton(aclass)def oncall(*args**kwargs)if oncall instance =noneoncall instance aclass(*args**kwargsreturn oncall instance oncall instance none return oncall on decoration on instance creation one function per class class singletondef __init__(selfaclass)on decoration self aclass aclass self instance none def __call__(self*args**kwargs)on instance creation if self instance =noneself instance self aclass(*args**kwargsone instance per class return self instance to make this decorator fully general-purpose toolchoose onestore it in an importable module fileand indent the self-test code under __name__ check--steps we'll leave as suggested exercise the final class-based version offers portability and explicit optionwith extra structure that may better support later evolutionbut oop might not be warranted in all contexts tracing object interfaces the singleton example of the prior section illustrated using class decorators to manage all the instances of class another common use case for class decorators augments the interface of each generated instance class decorators can essentially install on incoding class decorators