Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e471772f
Kaydet (Commit)
e471772f
authored
Agu 14, 2012
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Review of signature docs.
üst
1487c931
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
65 deletions
+62
-65
inspect.rst
Doc/library/inspect.rst
+62
-65
No files found.
Doc/library/inspect.rst
Dosyayı görüntüle @
e471772f
...
...
@@ -397,25 +397,18 @@ Retrieving source code
.. _inspect-signature-object:
Introspecting callables with Signature Object
---------------------------------------------
Signature object represents the call signature of a callable object and its
return annotation. To get a Signature object use the :func:`signature`
function.
Introspecting callables with the Signature object
-------------------------------------------------
.. versionadded:: 3.3
.. seealso::
:pep:`362` - Function Signature Object.
The detailed specification, implementation details and examples.
The Signature object represents the call signature of a callable object and its
return annotation. To retrieve a Signature object, use the :func:`signature`
function.
.. function:: signature(callable)
Return
s
a :class:`Signature` object for the given ``callable``::
Return a :class:`Signature` object for the given ``callable``::
>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
...
...
@@ -432,24 +425,24 @@ function.
>>> sig.parameters['b'].annotation
<class 'int'>
Accepts a wide range of python callables, from plain functions and classes
to
:func:`functools.partial` objects.
Accepts a wide range of python callables, from plain functions and classes
to
:func:`functools.partial` objects.
.. note::
Some callables may not be introspectable in certain implementations
of Python. For example, in CPython, built-in functions defined in C
provide
no metadata about their arguments.
Some callables may not be introspectable in certain implementations
of
Python. For example, in CPython, built-in functions defined in C provide
no metadata about their arguments.
.. class:: Signature
A Signature object represents the call signature of a function and its
return annotation. For each parameter accepted by the function it
stores a
:class:`Parameter` object in its :attr:`parameters` collection.
A Signature object represents the call signature of a function and its
return
annotation. For each parameter accepted by the function it stores a
:class:`Parameter` object in its :attr:`parameters` collection.
Signature objects are *immutable*.
Use :meth:`Signature.replace` to make
a
modified copy.
Signature objects are *immutable*.
Use :meth:`Signature.replace` to make a
modified copy.
.. attribute:: Signature.empty
...
...
@@ -462,30 +455,29 @@ function.
.. attribute:: Signature.return_annotation
The "return" annotation for the callable. If the callable has
no "return" annotation, this attribute is set to
:attr:`Signature.empty`.
The "return" annotation for the callable. If the callable has no "return"
annotation, this attribute is set to :attr:`Signature.empty`.
.. method:: Signature.bind(*args, **kwargs)
Create
s
a mapping from positional and keyword arguments to parameters.
Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match
the
signature, or raises a :exc:`TypeError`.
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`.
.. method:: Signature.bind_partial(*args, **kwargs)
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.
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.
.. method:: Signature.replace([parameters], *, [return_annotation])
Create
s a new Signature instance based on the instance replace was
invoked on.
It is possible to pass different ``parameters`` and/or
``return_annotation`` to override the corresponding properties of
the base signature. To remove return_annotation from the copied
Signature, pass in
:attr:`Signature.empty`.
Create
a new Signature instance based on the instance replace was invoked
on.
It is possible to pass different ``parameters`` and/or
``return_annotation`` to override the corresponding properties of
the base
signature. To remove return_annotation from the copied Signature, pass in
:attr:`Signature.empty`.
::
...
...
@@ -497,38 +489,36 @@ function.
"(a, b) -> 'new return anno'"
.. class:: Parameter
Parameter objects are *immutable*. Instead of modifying a Parameter object,
Parameter objects are *immutable*.
Instead of modifying a Parameter object,
you can use :meth:`Parameter.replace` to create a modified copy.
.. attribute:: Parameter.empty
A special class-level marker to specify absence of default
values and
annotations.
A special class-level marker to specify absence of default
values and
annotations.
.. attribute:: Parameter.name
The name of the parameter as a string. Must be a valid python identifier
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can
have it set to ``None``.)
The name of the parameter as a string.
Must be a valid python identifier
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can
have
it set to ``None``).
.. attribute:: Parameter.default
The default value for the parameter. If the parameter has no default
The default value for the parameter.
If the parameter has no default
value, this attribute is set to :attr:`Parameter.empty`.
.. attribute:: Parameter.annotation
The annotation for the parameter. If the parameter has no annotation,
The annotation for the parameter.
If the parameter has no annotation,
this attribute is set to :attr:`Parameter.empty`.
.. attribute:: Parameter.kind
Describes how argument values are bound to the parameter.
Possible values (accessible via :class:`Parameter`, like
``Parameter.KEYWORD_ONLY``):
Describes how argument values are bound to the parameter. Possible values
(accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``):
+------------------------+----------------------------------------------+
| Name | Meaning |
...
...
@@ -577,10 +567,10 @@ function.
.. method:: Parameter.replace(*, [name], [kind], [default], [annotation])
Create
s a new Parameter instance based on the instance replaced was
invoked on. To override a :class:`Parameter` attribute, pass the
corresponding argument. To remove a default value or/and an annotation
from a
Parameter, pass :attr:`Parameter.empty`.
Create
a new Parameter instance based on the instance replaced was invoked
on. To override a :class:`Parameter` attribute, pass the corresponding
argument. To remove a default value or/and an annotation from a
Parameter, pass :attr:`Parameter.empty`.
::
...
...
@@ -604,18 +594,18 @@ function.
.. attribute:: BoundArguments.arguments
An ordered, mutable mapping (:class:`collections.OrderedDict`) of
parameters' names to arguments' values.
Contains only explicitly
bound arguments. Changes in :attr:`arguments` will reflect in
:attr:`
args` and :attr:`
kwargs`.
parameters' names to arguments' values.
Contains only explicitly bound
arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and
:attr:`kwargs`.
Should be used in conjunction with :attr:`Signature.parameters` for
a
ny arguments
processing purposes.
Should be used in conjunction with :attr:`Signature.parameters` for
any
a
rgument
processing purposes.
.. note::
Arguments for which :meth:`Signature.bind` or
:meth:`Signature.bind_partial` relied on a default value are skipped.
However, if needed, it
's easy to include them
However, if needed, it
is easy to include them.
::
...
...
@@ -638,15 +628,16 @@ function.
.. attribute:: BoundArguments.args
Tuple of positional arguments values. Dynamically computed
from the
:attr:`arguments` attribute.
A tuple of positional arguments values. Dynamically computed from the
:attr:`arguments` attribute.
.. attribute:: BoundArguments.kwargs
Dict of keyword arguments values. Dynamically computed
from the
:attr:`arguments` attribute.
A dict of keyword arguments values. Dynamically computed from the
:attr:`arguments` attribute.
:attr:`args` and :attr:`kwargs` properties can be used to invoke functions::
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
functions::
def test(a, *, b):
...
...
...
@@ -656,6 +647,12 @@ function.
test(*ba.args, **ba.kwargs)
.. seealso::
:pep:`362` - Function Signature Object.
The detailed specification, implementation details and examples.
.. _inspect-classes-functions:
Classes and functions
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment