function parameter types and parameter bindings in Python

Source: Internet
Author: User

Parameter type

There are five types of parameters for the Python function, namely:

    • Positional_or_keyword(positional parameter or keyword parameter)
    • var_positional(variable parameter)
    • keyword_only(keyword parameter)
    • var_keyword(variable keyword parameter)
    • positional_only(positional parameters)

Here are a few examples to explain the meaning of these 5 parameter types:

      

Positional_or_keyword , as the name sees it, can be used either as a positional reference or as a keyword, and he has no * declaration

>>> def foo (name): ...     Print (name) ...>>> foo ("Hello") hello>>> foo (name= "Hello") hello

  

var_positional is a mutable parameter, declared by A *, which deposits the received value into a tuple

>>> def foo (*args): ...     Print (args) ...>>> foo (1, 2, 3, 4, 5) (1, 2, 3, 4, 5)

  

keyword_only can only be passed through the keyword, this parameter will be after the var_positional parameter type, and without a * * prefix, as semantics, can only be passed by the specified keyword, can not use the location to pass the parameter

>>> def foo (N1, *, N2): ...     Print (n1, N2) ...>>> foo ("Hello", n2= "World") Hello World

  

Var_keyword is a variable keyword parameter, declared by a prefix * *, which can receive 0 or more parameters and deposit a dictionary

>>> def foo (**kwargs): ...     For key, value in Kwargs.items ():         ... Print ("%s=%s"% (key, value)) ...>>> foo (a=1, b=2, c=3) a=1b=2c=3

  

Positional_only is the fifth parameter type, but it doesn't matter anymore because a high version of Python cannot create a parameter of type positional_only, However, some functions, such as DIVMOD, that are implemented with the C language and do not receive keyword arguments, are supported

From the following example, we can see that the new definition of the Foo function, each parameter corresponds to one of the above types

>>> def foo (name, *args, Middle=none, **kwargs):     ... Print ("Name:", name)     ... Print ("args:", args)     ... Print ("Middle:", middle)     ... Print ("Kwargs:", Kwargs) ...>>> foo ("Hello", 1, 2, 3, middle= "World", A=1, b=2, c=3) Name:helloargs: (1, 2, 3) Midd Le:worldkwargs: {' A ': 1, ' B ': 2, ' C ': 3}>>> My_foo = {"name": "Hello", "Middle": "World", "a": "1", "B": "2", "C ":" 3 "}>>> foo (**my_foo) Name:helloargs: () Middle:worldkwargs: {' A ': ' 1 ', ' B ': ' 2 ', ' C ': ' 3 '}>>> from I Nspect Import signature>>> sig = Signature (foo) >>> for name, param in Sig.parameters.items (): ...     Print (Param.kind, ":", name, ' = ', Param.default) ... Positional_or_keyword:name = <class ' inspect._empty ' >var_positional:args = <class ' Inspect._empty ' > Keyword_only:middle = Nonevar_keyword:kwargs = <class ' Inspect._empty ' >

  

Parameter binding

Bind the parameters of a function to a dictionary

>>> def foo (name, *args, Middle=none, **kwargs):     ... Print ("Name:", name)     ... Print ("args:", args)     ... Print ("Middle:", middle)     ... Print ("Kwargs:", Kwargs) ...>>> My_foo = {"name": "Hello", "Middle": "World", "a": "1", "B": "2", "C": "3"}>> ;> from inspect import signature>>> sig = Signature (foo) >>> Bound_args = Sig.bind (**my_foo) >> > for name, value in Bound_args.arguments.items ():     ... Print (name, ' = ', value) ... name = Hellomiddle = Worldkwargs = {' A ': ' 1 ', ' B ': ' 2 ', ' C ': ' 3 '}>>> del my_foo[' name '] >>> Bound_args = Sig.bind (**my_foo) Traceback (most recent call last): ... Typeerror:missing A required argument: ' Name '

  

With the help of the inspect module, the Python data model is shown to bind the arguments to the formal parameters of the function call, which is the same mechanism used by the interpreter, and when we delete the name in the dictionary, the error is missing when we execute the name parameter.

Function parameter types and parameter bindings in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.