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
...
@@ -397,25 +397,18 @@ Retrieving source code
.. _inspect-signature-object:
.. _inspect-signature-object:
Introspecting callables with Signature Object
Introspecting callables with the 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.
.. versionadded:: 3.3
.. versionadded:: 3.3
.. seealso::
The Signature object represents the call signature of a callable object and its
return annotation. To retrieve a Signature object, use the :func:`signature`
:pep:`362` - Function Signature Object.
function.
The detailed specification, implementation details and examples.
.. function:: signature(callable)
.. 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
>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
>>> def foo(a, *, b:int, **kwargs):
...
@@ -432,24 +425,24 @@ function.
...
@@ -432,24 +425,24 @@ function.
>>> sig.parameters['b'].annotation
>>> sig.parameters['b'].annotation
<class 'int'>
<class 'int'>
Accepts a wide range of python callables, from plain functions and classes
Accepts a wide range of python callables, from plain functions and classes
to
to
:func:`functools.partial` objects.
:func:`functools.partial` objects.
.. note::
.. note::
Some callables may not be introspectable in certain implementations
Some callables may not be introspectable in certain implementations
of
of Python. For example, in CPython, built-in functions defined in C
Python. For example, in CPython, built-in functions defined in C provide
provide
no metadata about their arguments.
no metadata about their arguments.
.. class:: Signature
.. class:: Signature
A Signature object represents the call signature of a function and its
A Signature object represents the call signature of a function and its
return
return annotation. For each parameter accepted by the function it
annotation. For each parameter accepted by the function it stores a
stores a
:class:`Parameter` object in its :attr:`parameters` collection.
:class:`Parameter` object in its :attr:`parameters` collection.
Signature objects are *immutable*.
Use :meth:`Signature.replace` to make
Signature objects are *immutable*.
Use :meth:`Signature.replace` to make a
a
modified copy.
modified copy.
.. attribute:: Signature.empty
.. attribute:: Signature.empty
...
@@ -462,30 +455,29 @@ function.
...
@@ -462,30 +455,29 @@ function.
.. attribute:: Signature.return_annotation
.. attribute:: Signature.return_annotation
The "return" annotation for the callable. If the callable has
The "return" annotation for the callable. If the callable has no "return"
no "return" annotation, this attribute is set to
annotation, this attribute is set to :attr:`Signature.empty`.
:attr:`Signature.empty`.
.. method:: Signature.bind(*args, **kwargs)
.. method:: Signature.bind(*args, **kwargs)
Create
s
a mapping from positional and keyword arguments to parameters.
Create a mapping from positional and keyword arguments to parameters.
Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match
Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match
the
the
signature, or raises a :exc:`TypeError`.
signature, or raises a :exc:`TypeError`.
.. method:: Signature.bind_partial(*args, **kwargs)
.. method:: Signature.bind_partial(*args, **kwargs)
Works the same way as :meth:`Signature.bind`, but allows the
Works the same way as :meth:`Signature.bind`, but allows the
omission of
omission of some required arguments (mimics :func:`functools.partial`
some required arguments (mimics :func:`functools.partial` behavior.)
behavior.) Returns :class:`BoundArguments`, or raises a :exc:`TypeError`
Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the
if the
passed arguments do not match the signature.
passed arguments do not match the signature.
.. method:: Signature.replace([parameters], *, [return_annotation])
.. method:: Signature.replace([parameters], *, [return_annotation])
Create
s a new Signature instance based on the instance replace was
Create
a new Signature instance based on the instance replace was invoked
invoked on.
It is possible to pass different ``parameters`` and/or
on.
It is possible to pass different ``parameters`` and/or
``return_annotation`` to override the corresponding properties of
``return_annotation`` to override the corresponding properties of
the base
the base signature. To remove return_annotation from the copied
signature. To remove return_annotation from the copied Signature, pass in
Signature, pass in
:attr:`Signature.empty`.
:attr:`Signature.empty`.
::
::
...
@@ -497,38 +489,36 @@ function.
...
@@ -497,38 +489,36 @@ function.
"(a, b) -> 'new return anno'"
"(a, b) -> 'new return anno'"
.. class:: Parameter
.. 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.
you can use :meth:`Parameter.replace` to create a modified copy.
.. attribute:: Parameter.empty
.. attribute:: Parameter.empty
A special class-level marker to specify absence of default
A special class-level marker to specify absence of default
values and
values and
annotations.
annotations.
.. attribute:: Parameter.name
.. attribute:: Parameter.name
The name of the parameter as a string. Must be a valid python identifier
The name of the parameter as a string.
Must be a valid python identifier
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can
have
have it set to ``None``.)
it set to ``None``).
.. attribute:: Parameter.default
.. 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`.
value, this attribute is set to :attr:`Parameter.empty`.
.. attribute:: Parameter.annotation
.. 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`.
this attribute is set to :attr:`Parameter.empty`.
.. attribute:: Parameter.kind
.. attribute:: Parameter.kind
Describes how argument values are bound to the parameter.
Describes how argument values are bound to the parameter. Possible values
Possible values (accessible via :class:`Parameter`, like
(accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``):
``Parameter.KEYWORD_ONLY``):
+------------------------+----------------------------------------------+
+------------------------+----------------------------------------------+
| Name | Meaning |
| Name | Meaning |
...
@@ -577,10 +567,10 @@ function.
...
@@ -577,10 +567,10 @@ function.
.. method:: Parameter.replace(*, [name], [kind], [default], [annotation])
.. method:: Parameter.replace(*, [name], [kind], [default], [annotation])
Create
s a new Parameter instance based on the instance replaced was
Create
a new Parameter instance based on the instance replaced was invoked
invoked on. To override a :class:`Parameter` attribute, pass the
on. To override a :class:`Parameter` attribute, pass the corresponding
corresponding argument. To remove a default value or/and an annotation
argument. To remove a default value or/and an annotation from a
from a
Parameter, pass :attr:`Parameter.empty`.
Parameter, pass :attr:`Parameter.empty`.
::
::
...
@@ -604,18 +594,18 @@ function.
...
@@ -604,18 +594,18 @@ function.
.. attribute:: BoundArguments.arguments
.. attribute:: BoundArguments.arguments
An ordered, mutable mapping (:class:`collections.OrderedDict`) of
An ordered, mutable mapping (:class:`collections.OrderedDict`) of
parameters' names to arguments' values.
Contains only explicitly
parameters' names to arguments' values.
Contains only explicitly bound
bound arguments. Changes in :attr:`arguments` will reflect in
arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and
:attr:`
args` and :attr:`
kwargs`.
:attr:`kwargs`.
Should be used in conjunction with :attr:`Signature.parameters` for
Should be used in conjunction with :attr:`Signature.parameters` for
any
a
ny arguments
processing purposes.
a
rgument
processing purposes.
.. note::
.. note::
Arguments for which :meth:`Signature.bind` or
Arguments for which :meth:`Signature.bind` or
:meth:`Signature.bind_partial` relied on a default value are skipped.
: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.
...
@@ -638,15 +628,16 @@ function.
.. attribute:: BoundArguments.args
.. attribute:: BoundArguments.args
Tuple of positional arguments values. Dynamically computed
A tuple of positional arguments values. Dynamically computed from the
from the
:attr:`arguments` attribute.
:attr:`arguments` attribute.
.. attribute:: BoundArguments.kwargs
.. attribute:: BoundArguments.kwargs
Dict of keyword arguments values. Dynamically computed
A dict of keyword arguments values. Dynamically computed from the
from the
:attr:`arguments` attribute.
: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):
def test(a, *, b):
...
...
...
@@ -656,6 +647,12 @@ function.
...
@@ -656,6 +647,12 @@ function.
test(*ba.args, **ba.kwargs)
test(*ba.args, **ba.kwargs)
.. seealso::
:pep:`362` - Function Signature Object.
The detailed specification, implementation details and examples.
.. _inspect-classes-functions:
.. _inspect-classes-functions:
Classes and 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