fork download
  1. class MyClass(object):
  2. _instance = None
  3.  
  4. @classmethod
  5. def get_instance(cls, *args, **kwargs):
  6. if cls._instance is None:
  7. cls._instance = super().__new__(cls, *args, **kwargs)
  8. cls.__init__(cls._instance, *args, **kwargs)
  9. return cls._instance
  10.  
  11. def __init__(self):
  12. #so something
  13. self.fun()
  14. #so something
  15.  
  16. def fun(self):
  17. print("Hello, World!")
  18.  
  19. print(MyClass.get_instance())
  20.  
Success #stdin #stdout 0.02s 28352KB
stdin
Standard input is empty
stdout
Hello, World!
<__main__.MyClass object at 0x2ba4cc72dd30>