Skip to content

Commit 1cc98b7

Browse files
miss-islingtonstefmolinDas-Chinmay
authored
[3.14] gh-72088: clarify inspect.ismethod and inspect.isfunction (and related) usage with class-level access (GH-150013) (GH-150033)
(cherry picked from commit 0aa59ce) Co-authored-by: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Co-authored-by: CHINMAY <89741289+Das-Chinmay@users.noreply.github.com>
1 parent fd1cf39 commit 1cc98b7

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

Doc/library/inspect.rst

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,24 +407,63 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
407407

408408
Return ``True`` if the object is a bound method written in Python.
409409

410+
.. note::
410411

411-
.. function:: ispackage(object)
412+
For example, given this class::
412413

413-
Return ``True`` if the object is a :term:`package`.
414+
>>> class Greeter:
415+
... def say_hello(self):
416+
... print('hello!')
414417

415-
.. versionadded:: 3.14
418+
A bound method (also known as an *instance method*) is created when
419+
accessing ``say_hello`` (a :term:`function` defined in the
420+
``Greeter`` namespace) through an instance of the ``Greeter`` class::
421+
422+
>>> instance = Greeter()
423+
424+
>>> instance.say_hello
425+
<bound method Greeter.say_hello of <__main__.Greeter object ...>>
426+
>>> ismethod(instance.say_hello)
427+
True
428+
>>> isfunction(instance.say_hello)
429+
False
430+
431+
Accessing ``say_hello`` through the ``Greeter`` class will return the
432+
function itself. For this function, :func:`ismethod` will return
433+
``False``, but :func:`isfunction` will return ``True``::
434+
435+
>>> Greeter.say_hello
436+
<function Greeter.say_hello at 0x7f7503854a90>
437+
>>> ismethod(Greeter.say_hello)
438+
False
439+
>>> isfunction(Greeter.say_hello)
440+
True
441+
442+
See :ref:`typesmethods` for details.
416443

417444

418445
.. function:: isfunction(object)
419446

420447
Return ``True`` if the object is a Python function, which includes functions
421448
created by a :term:`lambda` expression.
422449

450+
See the note for :func:`~inspect.ismethod` for an example.
451+
452+
453+
.. function:: ispackage(object)
454+
455+
Return ``True`` if the object is a :term:`package`.
456+
457+
.. versionadded:: 3.14
458+
423459

424460
.. function:: isgeneratorfunction(object)
425461

426462
Return ``True`` if the object is a Python generator function.
427463

464+
It also returns ``True`` for bound methods created from Python generator functions
465+
(see :ref:`typesmethods` for more information).
466+
428467
.. versionchanged:: 3.8
429468
Functions wrapped in :func:`functools.partial` now return ``True`` if the
430469
wrapped function is a Python generator function.

0 commit comments

Comments
 (0)