Python Glossary Русский

all Python 3inspectfunction

inspect.ismethod()

object kind checks

Checks that an object is a bound method written in Python: a function already tied to an instance or a class.

Syntax

inspect.ismethod(object) -> bool

A method appears only on access through an instance: a.m is a method, A.m is a plain function. A classmethod is bound to the class, so ismethod(A.cm) is True, while a staticmethod stays a function. Methods of built-in types ([].append) do not count, isbuiltin covers them. A method has __self__ (what it is bound to) and __func__ (the original function).

Examples

import inspect
class A:
    def m(self): pass
    @classmethod
    def cm(cls): pass
    @staticmethod
    def sm(): pass
a = A()
print(inspect.ismethod(a.m), inspect.ismethod(A.m))  # → True False
print(inspect.ismethod(A.cm), inspect.ismethod(A.sm))  # → True False
print(inspect.ismethod([].append), a.m.__self__ is a)  # → False True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy