From eb7be9ab52593f7a27d2046cd44b1fdfd6cac430 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 20 May 2026 03:00:54 +0200 Subject: [PATCH] [3.15] gh-72088: clarify `inspect.ismethod` and `inspect.isfunction` (and related) usage with class-level access (GH-150013) (GH-150119) (cherry picked from commit 0aa59ce2d4f007a9d19740eb2f6230ed302096f7) Co-authored-by: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Co-authored-by: CHINMAY <89741289+Das-Chinmay@users.noreply.github.com> --- Doc/library/inspect.rst | 51 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 48ae9147587..92840e702fb 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -424,6 +424,48 @@ attributes (see :ref:`import-mod-attrs` for module attributes): Return ``True`` if the object is a bound method written in Python. + .. note:: + + For example, given this class:: + + >>> class Greeter: + ... def say_hello(self): + ... print('hello!') + + A bound method (also known as an *instance method*) is created when + accessing ``say_hello`` (a :term:`function` defined in the + ``Greeter`` namespace) through an instance of the ``Greeter`` class:: + + >>> instance = Greeter() + + >>> instance.say_hello + > + >>> ismethod(instance.say_hello) + True + >>> isfunction(instance.say_hello) + False + + Accessing ``say_hello`` through the ``Greeter`` class will return the + function itself. For this function, :func:`ismethod` will return + ``False``, but :func:`isfunction` will return ``True``:: + + >>> Greeter.say_hello + + >>> ismethod(Greeter.say_hello) + False + >>> isfunction(Greeter.say_hello) + True + + See :ref:`typesmethods` for details. + + +.. function:: isfunction(object) + + Return ``True`` if the object is a Python function, which includes functions + created by a :term:`lambda` expression. + + See the note for :func:`~inspect.ismethod` for an example. + .. function:: ispackage(object) @@ -432,16 +474,13 @@ attributes (see :ref:`import-mod-attrs` for module attributes): .. versionadded:: 3.14 -.. function:: isfunction(object) - - Return ``True`` if the object is a Python function, which includes functions - created by a :term:`lambda` expression. - - .. function:: isgeneratorfunction(object) Return ``True`` if the object is a Python generator function. + It also returns ``True`` for bound methods created from Python generator functions + (see :ref:`typesmethods` for more information). + .. versionchanged:: 3.8 Functions wrapped in :func:`functools.partial` now return ``True`` if the wrapped function is a Python generator function.