Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e71362d

Browse files
committed
Issue #10518: Bring back the callable() builtin.
Approved by Guido (BDFL) and Georg (RM).
1 parent dc9b17d commit e71362d

5 files changed

Lines changed: 61 additions & 14 deletions

File tree

Doc/library/functions.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ are always available. They are listed here in alphabetical order.
121121
Bytes objects can also be created with literals, see :ref:`strings`.
122122

123123

124+
.. function:: callable(object)
125+
126+
Return :const:`True` if the *object* argument appears callable,
127+
:const:`False` if not. If this returns true, it is still possible that a
128+
call fails, but if it is false, calling *object* will never succeed.
129+
Note that classes are callable (calling a class returns a new instance);
130+
instances are callable if their class has a :meth:`__call__` method.
131+
132+
.. versionadded:: 3.2
133+
This function was first removed in Python 3.0 and then brought back
134+
in Python 3.2.
135+
136+
124137
.. function:: chr(i)
125138

126139
Return the string representing a character whose Unicode codepoint is the integer

Lib/test/test_bool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ def test_hasattr(self):
174174
self.assertIs(hasattr([], "wobble"), False)
175175

176176
def test_callable(self):
177-
self.assertIs(hasattr(len, '__call__'), True)
178-
self.assertIs(hasattr(1, '__call__'), False)
177+
self.assertIs(callable(len), True)
178+
self.assertIs(callable(1), False)
179179

180180
def test_isinstance(self):
181181
self.assertIs(isinstance(True, bool), True)

Lib/test/test_builtin.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,39 @@ def test_neg(self):
207207
self.assertTrue(isinstance(x, int))
208208
self.assertEqual(-x, sys.maxsize+1)
209209

210-
# XXX(nnorwitz): This test case for callable should probably be removed.
211210
def test_callable(self):
212-
self.assertTrue(hasattr(len, '__call__'))
211+
self.assertTrue(callable(len))
212+
self.assertFalse(callable("a"))
213+
self.assertTrue(callable(callable))
214+
self.assertTrue(callable(lambda x, y: x + y))
215+
self.assertFalse(callable(__builtins__))
213216
def f(): pass
214-
self.assertTrue(hasattr(f, '__call__'))
215-
class C:
217+
self.assertTrue(callable(f))
218+
219+
class C1:
216220
def meth(self): pass
217-
self.assertTrue(hasattr(C, '__call__'))
218-
x = C()
219-
self.assertTrue(hasattr(x.meth, '__call__'))
220-
self.assertTrue(not hasattr(x, '__call__'))
221-
class D(C):
221+
self.assertTrue(callable(C1))
222+
c = C1()
223+
self.assertTrue(callable(c.meth))
224+
self.assertFalse(callable(c))
225+
226+
# __call__ is looked up on the class, not the instance
227+
c.__call__ = None
228+
self.assertFalse(callable(c))
229+
c.__call__ = lambda self: 0
230+
self.assertFalse(callable(c))
231+
del c.__call__
232+
self.assertFalse(callable(c))
233+
234+
class C2(object):
222235
def __call__(self): pass
223-
y = D()
224-
self.assertTrue(hasattr(y, '__call__'))
225-
y()
236+
c2 = C2()
237+
self.assertTrue(callable(c2))
238+
c2.__call__ = None
239+
self.assertTrue(callable(c2))
240+
class C3(C2): pass
241+
c3 = C3()
242+
self.assertTrue(callable(c3))
226243

227244
def test_chr(self):
228245
self.assertEqual(chr(32), ' ')

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.2 Beta 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10518: Bring back the callable() builtin.
14+
1315
- Issue #8879. Add os.link support for Windows.
1416

1517
- Issue #10027. st_nlink was not being set on Windows calls to os.stat or

Python/bltinmodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ PyDoc_STRVAR(bin_doc,
311311
Return the binary representation of an integer or long integer.");
312312

313313

314+
static PyObject *
315+
builtin_callable(PyObject *self, PyObject *v)
316+
{
317+
return PyBool_FromLong((long)PyCallable_Check(v));
318+
}
319+
320+
PyDoc_STRVAR(callable_doc,
321+
"callable(object) -> bool\n\
322+
\n\
323+
Return whether the object is callable (i.e., some kind of function).\n\
324+
Note that classes are callable, as are instances of classes with a\n\
325+
__call__() method.");
326+
327+
314328
typedef struct {
315329
PyObject_HEAD
316330
PyObject *func;
@@ -2242,6 +2256,7 @@ static PyMethodDef builtin_methods[] = {
22422256
{"any", builtin_any, METH_O, any_doc},
22432257
{"ascii", builtin_ascii, METH_O, ascii_doc},
22442258
{"bin", builtin_bin, METH_O, bin_doc},
2259+
{"callable", builtin_callable, METH_O, callable_doc},
22452260
{"chr", builtin_chr, METH_VARARGS, chr_doc},
22462261
{"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
22472262
{"delattr", builtin_delattr, METH_VARARGS, delattr_doc},

0 commit comments

Comments
 (0)