fork download
  1. import inspect
  2.  
  3. class BaseMeta(type):
  4.  
  5. def __init__(cls, name, bases, namespace):
  6. super().__init__(name, bases, namespace)
  7. # check if cls.f is a static method
  8. if not inspect.isfunction(cls.f):
  9. raise Exception("f should be a static method")
  10. # check if cls.g is a static method
  11. if not (inspect.ismethod(cls.g) and cls.g.__self__ == cls):
  12. raise Exception("g should be a class method")
  13.  
  14. class Base(metaclass=BaseMeta):
  15.  
  16. @staticmethod
  17. def f():
  18. print("Base.f")
  19.  
  20. @classmethod
  21. def g(cls):
  22. print("Base.g")
  23.  
  24. def h(self):
  25. print("Base.h")
  26.  
  27. class A(Base):
  28.  
  29. def f():
  30. print("A.f")
  31.  
  32. class B(Base):
  33.  
  34. def g(cls):
  35. print("B.g")
Runtime error #stdin #stdout #stderr 0.02s 30704KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 32, in <module>
  File "./prog.py", line 12, in __init__
Exception: g should be a class method