fork(3) download
  1. class MyMeta(type):
  2.  
  3. def __init__(cls, name, bases, dct):
  4. super(MyMeta, cls).__init__(name, bases, dct)
  5. attributes = dict(cls.__dict__)
  6. user_attrs = [k for k, _ in attributes.items() if not (k.startswith('_') and k.endswith('_'))]
  7. for attr in user_attrs:
  8. def make_method(name):
  9. def _method(self):
  10. return getattr(self, name.split('_')[1])
  11.  
  12. return _method
  13.  
  14. setattr(cls, f'get_{attr[0]}', make_method(f'get_{attr[0]}'))
  15.  
  16.  
  17. class Kek(metaclass=MyMeta):
  18. a = 10
  19. b = 20
  20. c = 30
  21.  
  22.  
  23. if __name__ == '__main__':
  24. kek = Kek()
  25. print(kek.get_a())
  26. print(kek.get_b())
  27. print(kek.get_c())
  28.  
Success #stdin #stdout 0.02s 8948KB
stdin
Standard input is empty
stdout
10
20
30