Pelokalan
Python ID
id en
Masuk
Proyek
library__inspect.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.
Types and members
Type
Typ
__type_params__
A tuple containing the :ref:`type parameters <type-params>` of a generic class
A tuple containing the :ref:`type parameters <type-params>` of a generic function
This function returns ``False`` for :ref:`generic aliases <types-genericalias>` of classes, such as ``list[int]``.
See :ref:`typesmethods` for details.
It also returns ``True`` for bound methods created from Python generator functions (see :ref:`typesmethods` for more information).
:term:`Duck-typed <duck-typing>` function-like objects now return ``True`` if their code object has the :data:`CO_GENERATOR` flag.
:term:`Duck-typed <duck-typing>` function-like objects now return ``True`` if their code object has the :data:`CO_COROUTINE` flag.
import types def gen(): yield @types.coroutine def gen_coro(): yield assert not isawaitable(gen()) assert isawaitable(gen_coro())
:term:`Duck-typed <duck-typing>` function-like objects now return ``True`` if their code object has the :data:`CO_ASYNC_GENERATOR` flag.
Return ``True`` if the type of object is a :class:`~types.MethodWrapperType`.
These are instances of :class:`~types.MethodWrapperType`, such as :meth:`~object.__str__`, :meth:`~object.__eq__` and :meth:`~object.__repr__`.
getsets are attributes defined in extension modules via :c:type:`PyGetSetDef` structures. For Python implementations without such types, this method will always return ``False``.
Member descriptors are attributes defined in extension modules via :c:type:`PyMemberDef` structures. For Python implementations without such types, this method will always return ``False``.
Return the name of the (text or binary) file in which an object was defined. An :exc:`OSError` is raised if the source code cannot be retrieved. This will fail with a :exc:`TypeError` if the object is a built-in module, class, or function.
Return the name of the Python source file in which an object was defined or ``None`` if no way can be identified to get the source. An :exc:`OSError` is raised if the source code cannot be retrieved. This will fail with a :exc:`TypeError` if the object is a built-in module, class, or function.
Return a list of source lines and starting line number for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a list of the lines corresponding to the object and the line number indicates where in the original source file the first line of code was found. An :exc:`OSError` is raised if the source code cannot be retrieved. A :exc:`TypeError` is raised if the object is a built-in module, class, or function.
Return the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An :exc:`OSError` is raised if the source code cannot be retrieved. A :exc:`TypeError` is raised if the object is a built-in module, class, or function.
Raises :exc:`ValueError` if no signature can be provided, and :exc:`TypeError` if that type of object is not supported. Also, if the annotations are stringized, and *eval_str* is not false, the ``eval()`` call(s) to un-stringize the annotations in :func:`annotationlib.get_annotations` could potentially raise any kind of exception.
Create a mapping from positional and keyword arguments to parameters. Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the signature, or raises a :exc:`TypeError`.
Works the same way as :meth:`Signature.bind`, but allows the omission of some required arguments (mimics :func:`functools.partial` behavior.) Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the passed arguments do not match the signature.
:attr:`arguments` is now of type :class:`dict`. Formerly, it was of type :class:`collections.OrderedDict`.
Return a tuple of class cls's base classes, including cls, in method resolution order. No class appears more than once in this tuple. Note that the method resolution order depends on cls's type. Unless a very peculiar user-defined metatype is in use, cls will be the first element of the tuple.
Bind the *args* and *kwds* to the argument names of the Python function or method *func*, as if it was called with them. For bound methods, bind also the first argument (typically named ``self``) to the associated instance. A dict is returned, mapping the argument names (including the names of the ``*`` and ``**`` arguments, if any) to their values from *args* and *kwds*. In case of invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise an exception because of incompatible signature, an exception of the same type and the same or similar message is raised. For example:
>>> from inspect import getcallargs >>> def f(a, b=1, *pos, **named): ... pass ... >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} True >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} True >>> getcallargs(f) Traceback (most recent call last): ... TypeError: f() missing 1 required positional argument: 'a'
:exc:`TypeError` is raised if *func* is not a Python function or method.
# example code for resolving the builtin descriptor types class _foo: __slots__ = ['foo'] slot_descriptor = type(_foo.foo) getset_descriptor = type(type(open(__file__)).name) wrapper_descriptor = type(str.__dict__['__add__']) descriptor_types = (slot_descriptor, getset_descriptor, wrapper_descriptor) result = getattr_static(some_object, 'foo') if type(result) in descriptor_types: try: result = result.__get__() except AttributeError: # descriptors can raise AttributeError to # indicate there is no underlying value # in which case the descriptor itself will # have to do pass
If *generator* is a :term:`generator` with no currently associated frame, then an empty dictionary is returned. :exc:`TypeError` is raised if *generator* is not a Python generator object.
The meaning of the flags is explained at :ref:`buffer-request-types`.