1
0

47_reflection.py 453 B

123456789101112131415161718192021222324
  1. assert type(1) is int
  2. assert type(1.0) is float
  3. assert type(object) is type
  4. assert type(type) is type
  5. assert hasattr(object, '__base__')
  6. assert hasattr(1, '__add__')
  7. assert hasattr(int, '__add__')
  8. assert type(1).__add__(1, 2) == 3
  9. assert getattr(1, '__add__')(2) == 3
  10. a = object()
  11. setattr(a, 'b', 1)
  12. assert a.b == 1
  13. assert getattr(a, 'b') == 1
  14. try:
  15. getattr(a, 'xxx')
  16. exit(1)
  17. except AttributeError:
  18. pass
  19. assert getattr(a, 'xxx', 1) == 1