Python Glossary Русский

all Python 3inspectfunction

inspect.iscode()

stack and frames

Checks that an object is a code object (types.CodeType): the compiled body of a function or the result of compile().

Syntax

inspect.iscode(object) -> bool

A function and its code are different things: iscode(f) is False while iscode(f.__code__) is True. Code is immutable and knows neither globals nor default values, which belong to the function, so one code object can sit inside several functions. Every frame has one too (frame.f_code): that is how you get the running function's name via co_name.

Examples

import inspect
def f():
    return 1
print(inspect.iscode(f), inspect.iscode(f.__code__))  # → False True
print(inspect.iscode(compile('1 + 1', '<s>', 'eval')))  # → True
print(f.__code__.co_name)  # → f

See also

Open in the full glossarydocs.python.orgReport an inaccuracy