defisabstract(object):"""Return true if the object is an abstract base class (ABC)."""ifnotisinstance(object,type):returnFalseifobject.__flags__&TPFLAGS_IS_ABSTRACT:returnTrueifnotissubclass(type(object),abc.ABCMeta):returnFalseifhasattr(object,'__abstractmethods__'):# It looks like ABCMeta.__new__ has finished running;# TPFLAGS_IS_ABSTRACT should have been accurate.returnFalse# It looks like ABCMeta.__new__ has not finished running yet; we're# probably in __init_subclass__. We'll look for abstractmethods manually.forname,valueinobject.__dict__.items():ifgetattr(value,"__isabstractmethod__",False):returnTrueforbaseinobject.__bases__:fornameingetattr(base,"__abstractmethods__",()):value=getattr(object,name,None)ifgetattr(value,"__isabstractmethod__",False):returnTruereturnFalse
defisasyncgenfunction(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)
Return true if object can be passed to an await expression.
Example
Jinja call:
{%iffilters.joinisawaitable%}True!{%endif%}
Result:
DocStrings
Source code in python3.12/inspect.py
468469470471472473
defisawaitable(object):"""Return true if object can be passed to an ``await`` expression."""return(isinstance(object,types.CoroutineType)orisinstance(object,types.GeneratorType)andbool(object.gi_code.co_flags&CO_ITERABLE_COROUTINE)orisinstance(object,collections.abc.Awaitable))
defiscode(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"""returnisinstance(object,types.CodeType)
defiscoroutinefunction(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)
defisdatadescriptor(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."""ifisclass(object)orismethod(object)orisfunction(object):# mutual exclusionreturnFalsetp=type(object)returnhasattr(tp,"__set__")orhasattr(tp,"__delete__")
defisframe(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"""returnisinstance(object,types.FrameType)
Return true if the object is a user-defined function.
Example
Jinja call:
{%iffilters.joinisfunction%}True!{%endif%}
Result:
True!
DocStrings
Source code in python3.12/inspect.py
378379380381382383384385386387388389
defisfunction(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"""returnisinstance(object,types.FunctionType)
defisgenerator(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"""returnisinstance(object,types.GeneratorType)
defisgeneratorfunction(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)
defisgetsetdescriptor(object):"""Return true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules."""returnFalse
defismemberdescriptor(object):"""Return true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules."""returnFalse
defismethoddescriptor(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()."""ifisclass(object)orismethod(object)orisfunction(object):# mutual exclusionreturnFalsetp=type(object)returnhasattr(tp,"__get__")andnothasattr(tp,"__set__")
Return true if the object is any kind of function or method.
Example
Jinja call:
{%iffilters.joinisroutine%}True!{%endif%}
Result:
True!
DocStrings
Source code in python3.12/inspect.py
537538539540541542543
defisroutine(object):"""Return true if the object is any kind of function or method."""return(isbuiltin(object)orisfunction(object)orismethod(object)orismethoddescriptor(object)orismethodwrapper(object))
defistraceback(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)"""returnisinstance(object,types.TracebackType)