code_junkie

What is a “callable” in Python?

Nov 11th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. /* Test whether an object can be called */
  2.  
  3. int
  4. PyCallable_Check(PyObject *x)
  5. {
  6. if (x == NULL)
  7. return 0;
  8. if (PyInstance_Check(x)) {
  9. PyObject *call = PyObject_GetAttrString(x, "__call__");
  10. if (call == NULL) {
  11. PyErr_Clear();
  12. return 0;
  13. }
  14. /* Could test recursively but don't, for fear of endless
  15. recursion if some joker sets self.__call__ = self */
  16. Py_DECREF(call);
  17. return 1;
  18. }
  19. else {
  20. return x->ob_type->tp_call != NULL;
  21. }
  22. }
  23.  
  24. class Cached:
  25. def __init__(self, function):
  26. self.function = function
  27. self.cache = {}
  28.  
  29. def __call__(self, *args):
  30. try: return self.cache[args]
  31. except KeyError:
  32. ret = self.cache[args] = self.function(*args)
  33. return ret
  34.  
  35. @Cached
  36. def ack(x, y):
  37. return ack(x-1, ack(x, y-1)) if x*y else (x + y + 1)
  38.  
  39. class Quitter(object):
  40. def __init__(self, name):
  41. self.name = name
  42. def __repr__(self):
  43. return 'Use %s() or %s to exit' % (self.name, eof)
  44. def __call__(self, code=None):
  45. # Shells like IDLE catch the SystemExit, but listen when their
  46. # stdin wrapper is closed.
  47. try:
  48. sys.stdin.close()
  49. except:
  50. pass
  51. raise SystemExit(code)
  52. __builtin__.quit = Quitter('quit')
  53. __builtin__.exit = Quitter('exit')
  54.  
  55. class Foo:
  56. def __call__(self):
  57. print 'called'
  58.  
  59. foo_instance = Foo()
  60. foo_instance() #this is calling the __call__ method
  61.  
  62. >>> type(int).__call__(int)
  63. 0
  64. >>> type(object).__call__(object)
  65. <object object at 0x523460>
  66.  
  67. class a(object):
  68. def __call__(self, *args):
  69. print 'Hello'
  70.  
  71. func = a()
  72.  
  73. # or ...
  74. def func(*args):
  75. print 'Hello'
  76.  
  77. class Adder(object):
  78. def __init__(self, val):
  79. self.val = val
  80.  
  81. def __call__(self, val):
  82. return self.val + val
  83.  
  84. func = Adder(5)
  85. print func(3)
  86.  
  87. [nigel@k9 ~]$ python
  88. Python 2.5 (r25:51908, Nov 6 2007, 15:55:44)
  89. [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
  90. Type "help", "copyright", "credits" or "license" for more information.
  91. >>> 'aaa'() # <== Here we attempt to call a string.
  92. Traceback (most recent call last):
  93. File "<stdin>", line 1, in <module>
  94. TypeError: 'str' object is not callable
  95. >>>
  96.  
  97. >>> class Foo:
  98. ... pass
  99. ...
  100. >>> class Bar(object):
  101. ... pass
  102. ...
  103. >>> type(Foo).__call__(Foo)
  104. <__main__.Foo instance at 0x711440>
  105. >>> type(Bar).__call__(Bar)
  106. <__main__.Bar object at 0x712110>
  107. >>> def foo(bar):
  108. ... return bar
  109. ...
  110. >>> type(foo).__call__(foo, 42)
  111. 42
  112.  
  113. >>> class Foo(object):
  114. ... def __call__(self):
  115. ... return 42
  116. ...
  117. >>> f = Foo()
  118. >>> f()
  119. 42
Add Comment
Please, Sign In to add comment