970_inspect.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from inspect import isgeneratorfunction
  2. def f(a, b):
  3. return a + b
  4. assert not isgeneratorfunction(f)
  5. def g(a, b):
  6. yield a
  7. yield b
  8. assert isgeneratorfunction(g)
  9. class A:
  10. @staticmethod
  11. def non_gen(a, b):
  12. return a + b
  13. @staticmethod
  14. def gen(a, b):
  15. yield a
  16. yield b
  17. @classmethod
  18. def non_gen_class(cls, a, b):
  19. return a + b
  20. @classmethod
  21. def gen_class(cls, a, b):
  22. yield a
  23. yield b
  24. def not_gen_instance(self, a, b):
  25. return a + b
  26. def gen_instance(self, a, b):
  27. yield a
  28. yield b
  29. a = A()
  30. assert not isgeneratorfunction(a.non_gen)
  31. assert isgeneratorfunction(a.gen)
  32. assert not isgeneratorfunction(A.non_gen)
  33. assert isgeneratorfunction(A.gen)
  34. assert not isgeneratorfunction(a.non_gen_class)
  35. assert isgeneratorfunction(a.gen_class)
  36. assert not isgeneratorfunction(A.non_gen_class)
  37. assert isgeneratorfunction(A.gen_class)
  38. assert not isgeneratorfunction(a.not_gen_instance)
  39. assert isgeneratorfunction(a.gen_instance)
  40. assert not isgeneratorfunction(A.not_gen_instance)
  41. assert isgeneratorfunction(A.gen_instance)