Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e471772

Browse files
committed
Review of signature docs.
1 parent 1487c93 commit e471772

1 file changed

Lines changed: 62 additions & 65 deletions

File tree

Doc/library/inspect.rst

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -397,25 +397,18 @@ Retrieving source code
397397

398398
.. _inspect-signature-object:
399399

400-
Introspecting callables with Signature Object
401-
---------------------------------------------
402-
403-
Signature object represents the call signature of a callable object and its
404-
return annotation. To get a Signature object use the :func:`signature`
405-
function.
406-
400+
Introspecting callables with the Signature object
401+
-------------------------------------------------
407402

408403
.. versionadded:: 3.3
409404

410-
.. seealso::
411-
412-
:pep:`362` - Function Signature Object.
413-
The detailed specification, implementation details and examples.
414-
405+
The Signature object represents the call signature of a callable object and its
406+
return annotation. To retrieve a Signature object, use the :func:`signature`
407+
function.
415408

416409
.. function:: signature(callable)
417410

418-
Returns a :class:`Signature` object for the given ``callable``::
411+
Return a :class:`Signature` object for the given ``callable``::
419412

420413
>>> from inspect import signature
421414
>>> def foo(a, *, b:int, **kwargs):
@@ -432,24 +425,24 @@ function.
432425
>>> sig.parameters['b'].annotation
433426
<class 'int'>
434427

435-
Accepts a wide range of python callables, from plain functions and classes
436-
to :func:`functools.partial` objects.
428+
Accepts a wide range of python callables, from plain functions and classes to
429+
:func:`functools.partial` objects.
437430

438431
.. note::
439432

440-
Some callables may not be introspectable in certain implementations
441-
of Python. For example, in CPython, built-in functions defined in C
442-
provide no metadata about their arguments.
433+
Some callables may not be introspectable in certain implementations of
434+
Python. For example, in CPython, built-in functions defined in C provide
435+
no metadata about their arguments.
443436

444437

445438
.. class:: Signature
446439

447-
A Signature object represents the call signature of a function and its
448-
return annotation. For each parameter accepted by the function it
449-
stores a :class:`Parameter` object in its :attr:`parameters` collection.
440+
A Signature object represents the call signature of a function and its return
441+
annotation. For each parameter accepted by the function it stores a
442+
:class:`Parameter` object in its :attr:`parameters` collection.
450443

451-
Signature objects are *immutable*. Use :meth:`Signature.replace` to make
452-
a modified copy.
444+
Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
445+
modified copy.
453446

454447
.. attribute:: Signature.empty
455448

@@ -462,30 +455,29 @@ function.
462455

463456
.. attribute:: Signature.return_annotation
464457

465-
The "return" annotation for the callable. If the callable has
466-
no "return" annotation, this attribute is set to
467-
:attr:`Signature.empty`.
458+
The "return" annotation for the callable. If the callable has no "return"
459+
annotation, this attribute is set to :attr:`Signature.empty`.
468460

469461
.. method:: Signature.bind(*args, **kwargs)
470462

471-
Creates a mapping from positional and keyword arguments to parameters.
472-
Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match
473-
the signature, or raises a :exc:`TypeError`.
463+
Create a mapping from positional and keyword arguments to parameters.
464+
Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the
465+
signature, or raises a :exc:`TypeError`.
474466

475467
.. method:: Signature.bind_partial(*args, **kwargs)
476468

477-
Works the same way as :meth:`Signature.bind`, but allows the
478-
omission of some required arguments (mimics :func:`functools.partial`
479-
behavior.) Returns :class:`BoundArguments`, or raises a :exc:`TypeError`
480-
if the passed arguments do not match the signature.
469+
Works the same way as :meth:`Signature.bind`, but allows the omission of
470+
some required arguments (mimics :func:`functools.partial` behavior.)
471+
Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the
472+
passed arguments do not match the signature.
481473

482474
.. method:: Signature.replace([parameters], *, [return_annotation])
483475

484-
Creates a new Signature instance based on the instance replace was
485-
invoked on. It is possible to pass different ``parameters`` and/or
486-
``return_annotation`` to override the corresponding properties of
487-
the base signature. To remove return_annotation from the copied
488-
Signature, pass in :attr:`Signature.empty`.
476+
Create a new Signature instance based on the instance replace was invoked
477+
on. It is possible to pass different ``parameters`` and/or
478+
``return_annotation`` to override the corresponding properties of the base
479+
signature. To remove return_annotation from the copied Signature, pass in
480+
:attr:`Signature.empty`.
489481

490482
::
491483

@@ -497,38 +489,36 @@ function.
497489
"(a, b) -> 'new return anno'"
498490

499491

500-
501492
.. class:: Parameter
502493

503-
Parameter objects are *immutable*. Instead of modifying a Parameter object,
494+
Parameter objects are *immutable*. Instead of modifying a Parameter object,
504495
you can use :meth:`Parameter.replace` to create a modified copy.
505496

506497
.. attribute:: Parameter.empty
507498

508-
A special class-level marker to specify absence of default
509-
values and annotations.
499+
A special class-level marker to specify absence of default values and
500+
annotations.
510501

511502
.. attribute:: Parameter.name
512503

513-
The name of the parameter as a string. Must be a valid python identifier
514-
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can
515-
have it set to ``None``.)
504+
The name of the parameter as a string. Must be a valid python identifier
505+
name (with the exception of ``POSITIONAL_ONLY`` parameters, which can have
506+
it set to ``None``).
516507

517508
.. attribute:: Parameter.default
518509

519-
The default value for the parameter. If the parameter has no default
510+
The default value for the parameter. If the parameter has no default
520511
value, this attribute is set to :attr:`Parameter.empty`.
521512

522513
.. attribute:: Parameter.annotation
523514

524-
The annotation for the parameter. If the parameter has no annotation,
515+
The annotation for the parameter. If the parameter has no annotation,
525516
this attribute is set to :attr:`Parameter.empty`.
526517

527518
.. attribute:: Parameter.kind
528519

529-
Describes how argument values are bound to the parameter.
530-
Possible values (accessible via :class:`Parameter`, like
531-
``Parameter.KEYWORD_ONLY``):
520+
Describes how argument values are bound to the parameter. Possible values
521+
(accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``):
532522

533523
+------------------------+----------------------------------------------+
534524
| Name | Meaning |
@@ -577,10 +567,10 @@ function.
577567

578568
.. method:: Parameter.replace(*, [name], [kind], [default], [annotation])
579569

580-
Creates a new Parameter instance based on the instance replaced was
581-
invoked on. To override a :class:`Parameter` attribute, pass the
582-
corresponding argument. To remove a default value or/and an annotation
583-
from a Parameter, pass :attr:`Parameter.empty`.
570+
Create a new Parameter instance based on the instance replaced was invoked
571+
on. To override a :class:`Parameter` attribute, pass the corresponding
572+
argument. To remove a default value or/and an annotation from a
573+
Parameter, pass :attr:`Parameter.empty`.
584574

585575
::
586576

@@ -604,18 +594,18 @@ function.
604594
.. attribute:: BoundArguments.arguments
605595

606596
An ordered, mutable mapping (:class:`collections.OrderedDict`) of
607-
parameters' names to arguments' values. Contains only explicitly
608-
bound arguments. Changes in :attr:`arguments` will reflect in
609-
:attr:`args` and :attr:`kwargs`.
597+
parameters' names to arguments' values. Contains only explicitly bound
598+
arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and
599+
:attr:`kwargs`.
610600

611-
Should be used in conjunction with :attr:`Signature.parameters` for
612-
any arguments processing purposes.
601+
Should be used in conjunction with :attr:`Signature.parameters` for any
602+
argument processing purposes.
613603

614604
.. note::
615605

616606
Arguments for which :meth:`Signature.bind` or
617607
:meth:`Signature.bind_partial` relied on a default value are skipped.
618-
However, if needed, it's easy to include them
608+
However, if needed, it is easy to include them.
619609

620610
::
621611

@@ -638,15 +628,16 @@ function.
638628

639629
.. attribute:: BoundArguments.args
640630

641-
Tuple of positional arguments values. Dynamically computed
642-
from the :attr:`arguments` attribute.
631+
A tuple of positional arguments values. Dynamically computed from the
632+
:attr:`arguments` attribute.
643633

644634
.. attribute:: BoundArguments.kwargs
645635

646-
Dict of keyword arguments values. Dynamically computed
647-
from the :attr:`arguments` attribute.
636+
A dict of keyword arguments values. Dynamically computed from the
637+
:attr:`arguments` attribute.
648638

649-
:attr:`args` and :attr:`kwargs` properties can be used to invoke functions::
639+
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
640+
functions::
650641

651642
def test(a, *, b):
652643
...
@@ -656,6 +647,12 @@ function.
656647
test(*ba.args, **ba.kwargs)
657648

658649

650+
.. seealso::
651+
652+
:pep:`362` - Function Signature Object.
653+
The detailed specification, implementation details and examples.
654+
655+
659656
.. _inspect-classes-functions:
660657

661658
Classes and functions

0 commit comments

Comments
 (0)