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.12/inspect.py
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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.12/inspect.py
443
444
445
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.12/inspect.py
435
436
437
438
439
440
441
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.12/inspect.py
468
469
470
471
472
473
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.12/inspect.py
302
303
304
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.12/inspect.py
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
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.12/inspect.py
464
465
466
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.12/inspect.py
427
428
429
430
431
432
433
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.12/inspect.py
330
331
332
333
334
335
336
337
338
339
340
341
342
def isdatadescriptor(object):
    """Return true if the object is a data descriptor.

    Data descriptors have a __set__ or a __delete__ attribute.  Examples are
    properties (defined in Python) and getsets and members (defined in C).
    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.12/inspect.py
485
486
487
488
489
490
491
492
493
494
495
496
497
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.12/inspect.py
378
379
380
381
382
383
384
385
386
387
388
389
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.12/inspect.py
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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
        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.12/inspect.py
402
403
404
405
406
407
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.12/inspect.py
371
372
373
374
375
376
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.12/inspect.py
354
355
356
357
358
359
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.12/inspect.py
306
307
308
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.12/inspect.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
def ismethoddescriptor(object):
    """Return true if the object is a method descriptor.

    But not if ismethod() or isclass() or isfunction() are true.

    This is new in Python 2.2, and, for example, is true of int.__add__.
    An object passing this test has a __get__ attribute but not a __set__
    attribute, but 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 return false from the ismethoddescriptor() test, simply because
    the other tests promise more -- you can, e.g., count on having the
    __func__ attribute (etc) when an object passes ismethod()."""
    if isclass(object) or ismethod(object) or isfunction(object):
        # mutual exclusion
        return False
    tp = type(object)
    return hasattr(tp, "__get__") and not hasattr(tp, "__set__")

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.12/inspect.py
533
534
535
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.12/inspect.py
298
299
300
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.12/inspect.py
537
538
539
540
541
542
543
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.12/inspect.py
475
476
477
478
479
480
481
482
483
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)