Skip to content

inspect (21)

abstract

abstract(object)

Return true if the object is an abstract base class (ABC).

Example

Jinja call:

{% if filters.join is abstract %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isabstract(object):
    """Return true if the object is an abstract base class (ABC)."""
    if not isinstance(object, type):
        return False
    if object.__flags__ & TPFLAGS_IS_ABSTRACT:
        return True
    if not issubclass(type(object), abc.ABCMeta):
        return False
    if hasattr(object, '__abstractmethods__'):
        # It looks like ABCMeta.__new__ has finished running;
        # TPFLAGS_IS_ABSTRACT should have been accurate.
        return False
    # It looks like ABCMeta.__new__ has not finished running yet; we're
    # probably in __init_subclass__. We'll look for abstractmethods manually.
    for name, value in object.__dict__.items():
        if getattr(value, "__isabstractmethod__", False):
            return True
    for base in object.__bases__:
        for name in getattr(base, "__abstractmethods__", ()):
            value = getattr(object, name, None)
            if getattr(value, "__isabstractmethod__", False):
                return True
    return False

asyncgen

asyncgen(object)

Return true if the object is an asynchronous generator.

Example

Jinja call:

{% if filters.join is asyncgen %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isasyncgen(object):
    """Return true if the object is an asynchronous generator."""
    return isinstance(object, types.AsyncGeneratorType)

asyncgenfunction

asyncgenfunction(obj)

Return true if the object is an asynchronous generator function.

Example

Jinja call:

{% if filters.join is asyncgenfunction %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isasyncgenfunction(obj):
    """Return true if the object is an asynchronous generator function.

    Asynchronous generator functions are defined with "async def"
    syntax and have "yield" expressions in their body.
    """
    return _has_code_flag(obj, CO_ASYNC_GENERATOR)

awaitable

awaitable(object)

Return true if object can be passed to an await expression.

Example

Jinja call:

{% if filters.join is awaitable %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isawaitable(object):
    """Return true if object can be passed to an ``await`` expression."""
    return (isinstance(object, types.CoroutineType) or
            isinstance(object, types.GeneratorType) and
                bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
            isinstance(object, collections.abc.Awaitable))

class

class(object)

Return true if the object is a class.

Example

Jinja call:

{% if cycler is class %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in python3.13/inspect.py
def isclass(object):
    """Return true if the object is a class."""
    return isinstance(object, type)

code

code(object)

Return true if the object is a code object.

Example

Jinja call:

{% if filters.join is code %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def iscode(object):
    """Return true if the object is a code object.

    Code objects provide these attributes:
        co_argcount         number of arguments (not including *, ** args
                            or keyword only arguments)
        co_code             string of raw compiled bytecode
        co_cellvars         tuple of names of cell variables
        co_consts           tuple of constants used in the bytecode
        co_filename         name of file in which this code object was created
        co_firstlineno      number of first line in Python source code
        co_flags            bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
                            | 16=nested | 32=generator | 64=nofree | 128=coroutine
                            | 256=iterable_coroutine | 512=async_generator
        co_freevars         tuple of names of free variables
        co_posonlyargcount  number of positional only arguments
        co_kwonlyargcount   number of keyword only arguments (not including ** arg)
        co_lnotab           encoded mapping of line numbers to bytecode indices
        co_name             name with which this code object was defined
        co_names            tuple of names other than arguments and function locals
        co_nlocals          number of local variables
        co_stacksize        virtual machine stack space required
        co_varnames         tuple of names of arguments and local variables"""
    return isinstance(object, types.CodeType)

coroutine

coroutine(object)

Return true if the object is a coroutine.

Example

Jinja call:

{% if filters.join is coroutine %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def iscoroutine(object):
    """Return true if the object is a coroutine."""
    return isinstance(object, types.CoroutineType)

coroutinefunction

coroutinefunction(obj)

Return true if the object is a coroutine function.

Example

Jinja call:

{% if filters.join is coroutinefunction %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def iscoroutinefunction(obj):
    """Return true if the object is a coroutine function.

    Coroutine functions are normally defined with "async def" syntax, but may
    be marked via markcoroutinefunction.
    """
    return _has_code_flag(obj, CO_COROUTINE) or _has_coroutine_mark(obj)

datadescriptor

datadescriptor(object)

Return true if the object is a data descriptor.

Example

Jinja call:

{% if filters.join is datadescriptor %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isdatadescriptor(object):
    """Return true if the object is a data descriptor.

    But not if ismethod(), isclass() or isfunction() is true.

    Data descriptors have a __set__ or a __delete__ attribute.  Examples are
    properties, getsets, and members.  For the latter two (defined only in C
    extension modules) more specific tests are available as well:
    isgetsetdescriptor() and ismemberdescriptor(), respectively.

    Typically, data descriptors will also have __name__ and __doc__ attributes
    (properties, getsets, and members have both of these attributes), but this
    is not guaranteed."""
    if isclass(object) or ismethod(object) or isfunction(object):
        # mutual exclusion
        return False
    tp = type(object)
    return hasattr(tp, "__set__") or hasattr(tp, "__delete__")

frame

frame(object)

Return true if the object is a frame object.

Example

Jinja call:

{% if filters.join is frame %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isframe(object):
    """Return true if the object is a frame object.

    Frame objects provide these attributes:
        f_back          next outer frame object (this frame's caller)
        f_builtins      built-in namespace seen by this frame
        f_code          code object being executed in this frame
        f_globals       global namespace seen by this frame
        f_lasti         index of last attempted instruction in bytecode
        f_lineno        current line number in Python source code
        f_locals        local namespace seen by this frame
        f_trace         tracing function for this frame, or None"""
    return isinstance(object, types.FrameType)

function

function(object)

Return true if the object is a user-defined function.

Example

Jinja call:

{% if filters.join is function %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in python3.13/inspect.py
def isfunction(object):
    """Return true if the object is a user-defined function.

    Function objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this function was defined
        __code__        code object containing compiled function bytecode
        __defaults__    tuple of any default values for arguments
        __globals__     global namespace in which this function was defined
        __annotations__ dict of parameter annotations
        __kwdefaults__  dict of keyword only parameters with defaults"""
    return isinstance(object, types.FunctionType)

generator

generator(object)

Return true if the object is a generator.

Example

Jinja call:

{% if filters.join is generator %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isgenerator(object):
    """Return true if the object is a generator.

    Generator objects provide these attributes:
        __iter__        defined to support iteration over container
        close           raises a new GeneratorExit exception inside the
                        generator to terminate the iteration
        gi_code         code object
        gi_frame        frame object or possibly None once the generator has
                        been exhausted
        gi_running      set to 1 when generator is executing, 0 otherwise
        gi_suspended    set to 1 when the generator is suspended at a yield point, 0 otherwise
        gi_yieldfrom    object being iterated by yield from or None
        next            return the next item from the container
        send            resumes the generator and "sends" a value that becomes
                        the result of the current yield-expression
        throw           used to raise an exception inside the generator"""
    return isinstance(object, types.GeneratorType)

generatorfunction

generatorfunction(obj)

Return true if the object is a user-defined generator function.

Example

Jinja call:

{% if filters.join is generatorfunction %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isgeneratorfunction(obj):
    """Return true if the object is a user-defined generator function.

    Generator function objects provide the same attributes as functions.
    See help(isfunction) for a list of attributes."""
    return _has_code_flag(obj, CO_GENERATOR)

getsetdescriptor

getsetdescriptor(object)

Return true if the object is a getset descriptor.

Example

Jinja call:

{% if filters.join is getsetdescriptor %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def isgetsetdescriptor(object):
    """Return true if the object is a getset descriptor.

    getset descriptors are specialized descriptors defined in extension
    modules."""
    return False

memberdescriptor

memberdescriptor(object)

Return true if the object is a member descriptor.

Example

Jinja call:

{% if filters.join is memberdescriptor %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def ismemberdescriptor(object):
    """Return true if the object is a member descriptor.

    Member descriptors are specialized descriptors defined in extension
    modules."""
    return False

method

method(object)

Return true if the object is an instance method.

Example

Jinja call:

{% if filters.join is method %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def ismethod(object):
    """Return true if the object is an instance method."""
    return isinstance(object, types.MethodType)

methoddescriptor

methoddescriptor(object)

Return true if the object is a method descriptor.

Example

Jinja call:

{% if filters.join is methoddescriptor %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def ismethoddescriptor(object):
    """Return true if the object is a method descriptor.

    But not if ismethod(), isclass() or isfunction() is true.

    An object passing this test (for example, int.__add__) has a __get__
    attribute, but not a __set__ attribute or a __delete__ attribute.
    Beyond that, the set of attributes varies; __name__ is usually
    sensible, and __doc__ often is.

    Methods implemented via descriptors that also pass one of the other
    tests (ismethod(), isclass(), isfunction()) make this function return
    false, simply because those other tests promise more -- you can, for
    example, count on having the __func__ attribute when an object passes
    ismethod()."""
    if isclass(object) or ismethod(object) or isfunction(object):
        # mutual exclusion
        return False
    if isinstance(object, functools.partial):
        # Lie for children.  The addition of partial.__get__
        # doesn't currently change the partial objects behaviour,
        # not counting a warning about future changes.
        return False
    tp = type(object)
    return (hasattr(tp, "__get__")
            and not hasattr(tp, "__set__")
            and not hasattr(tp, "__delete__"))

methodwrapper

methodwrapper(object)

Return true if the object is a method wrapper.

Example

Jinja call:

{% if filters.join is methodwrapper %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def ismethodwrapper(object):
    """Return true if the object is a method wrapper."""
    return isinstance(object, types.MethodWrapperType)

module

module(object)

Return true if the object is a module.

Example

Jinja call:

{% if filters is module %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def ismodule(object):
    """Return true if the object is a module."""
    return isinstance(object, types.ModuleType)

routine

routine(object)

Return true if the object is any kind of function or method.

Example

Jinja call:

{% if filters.join is routine %}
True!
{% endif %}
Result:
True!

DocStrings
Source code in python3.13/inspect.py
def isroutine(object):
    """Return true if the object is any kind of function or method."""
    return (isbuiltin(object)
            or isfunction(object)
            or ismethod(object)
            or ismethoddescriptor(object)
            or ismethodwrapper(object))

traceback

traceback(object)

Return true if the object is a traceback.

Example

Jinja call:

{% if filters.join is traceback %}
True!
{% endif %}
Result:

DocStrings
Source code in python3.13/inspect.py
def istraceback(object):
    """Return true if the object is a traceback.

    Traceback objects provide these attributes:
        tb_frame        frame object at this level
        tb_lasti        index of last attempted instruction in bytecode
        tb_lineno       current line number in Python source code
        tb_next         next inner traceback object (called by this level)"""
    return isinstance(object, types.TracebackType)