builtin_callable.py 858 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # test builtin callable
  2. # primitives should not be callable
  3. print(callable(None))
  4. print(callable(1))
  5. print(callable([]))
  6. print(callable("dfsd"))
  7. # modules should not be callabe
  8. import sys
  9. print(callable(sys))
  10. # builtins should be callable
  11. print(callable(callable))
  12. # lambdas should be callable
  13. print(callable(lambda:None))
  14. # user defined functions should be callable
  15. def f():
  16. pass
  17. print(callable(f))
  18. # types should be callable, but not instances
  19. class A:
  20. pass
  21. print(callable(A))
  22. print(callable(A()))
  23. # instances with __call__ method should be callable
  24. class B:
  25. def __call__(self):
  26. pass
  27. print(callable(B()))
  28. # this checks internal use of callable when extracting members from an instance
  29. class C:
  30. def f(self):
  31. return "A.f"
  32. class D:
  33. g = C() # g is a value and is not callable
  34. print(callable(D().g))
  35. print(D().g.f())