A brief analysis of the Python function parameter type __ function

Source: Internet
Author: User
Tags abs function definition in python
on the parameter types of Python functions

In the Python function definition, as of the current to 3.x version, there are five types of parameters: (Note: This article is based on Python 3.4.4) positional_or_keyword (position or keyword parameter) positional_only (positional parameters only) Keyword_only (keyword parameter only) var_positional (any number of positional parameters) Var_keyword (any number of keyword parameters)

First: Position or keyword parameters:
Definition of first document: Parameter.positional_or_keyword-value May to supplied as either a KEYWORD OR positional argument (this is the Tandard binding behaviour for functions implemented in Python.)

This is the parameter that runs two ways of passing: location and keywords.

def foo (arg1, arg2):
    print (arg1, arg2)
#以位置参数传递
foo (' One ', ' two ')
#output: One
two

# Passed as keyword parameter:
foo (arg1= ' one ', arg2= ' two ')
foo (arg2= ' two ', arg1= ' one ')
#output: One
two
one Two

#混合方式:
foo (' One ', arg2= ' two ')
#output: One
two

#注意: keyword parameters cannot be passed before positional arguments
foo (arg1= ' One ', ' two ')
#output:

    foo (arg1= ' One ', ' two ')
                        ^
syntaxerror:non-keyword arg after keyword arg

Second: Positional parameters only: Parameter.positional_only-value must be supplied as a positional argument.
Python has no explicit syntax for defining positional-only parameters, but many built-in and extension module functions (E Specially those that accept only one or two parameters) accept them.

This means that only positional parameters allow the receiving of positional parameters, and there is currently no function to allow the developer to define itself to receive the parameter, in short, this typically occurs in a Python built-in function, such as:

ABS ( -1) #1
abs (a= '-1 ') #TypeError: ABS () takes no keyword arguments

Third: Only keyword parameters: Parameter.keyword_only-value must be supplied as a KEYWORD argument. Keyword only parameters are those which after a "*" or "appear" *args in a Python function definition.

This means that the parameter can only be passed by keyword and is defined after "*" or "*args". For example:

def foo (arg1, *, arg2):
    print (arg1, arg2)

foo (' One ', arg2= ' two ') #one two

foo (' One ', ' two ') #TypeError: foo ( ) takes 1 positional argument but 2 were given

foo (' One ') #TypeError: foo () missing 1 required keyword-only: ' Arg2 '

#综上: When you define a keyword-only parameter, you must pass in the parameter and can only be passed by keyword, that is, the key-value alignment.

Fourth: Any number of positional parameters parameter.var_positional-a tuple of positional arguments that aren ' t bound to either other Parameter. This is corresponds to a ' *args ' parameter in a Python function definition.

This parameter is an asterisk "*" parameter, that is, can accept any number of parameters, these parameters will be placed in the tuple provided to the function.

def foo (arg1, Arg2, *ARG3):
    print (Arg1, arg2, Arg3)

foo (' Doc ', ' txt ', ' png ', ' exe ', ' JPEG ') #doc txt (' png ', ' exe '), ' JPEG ')

#若传入一个元祖提供给 *arg3.
foo (' Doc ', ' txt ', (' png ', ' exe ', ' JPEG ')) #doc txt (' png ', ' exe ', ' JPEG '),)

#所以要用 *tuple way into the Yuan zu to *arg3
Foo (' Doc ', ' txt ', * (' png ', ' exe ', ' JPEG ') #doc txt (' png ', ' exe ', ' JPEG ')

Fifth: Any number of keyword parameters: parameter.var_keyword-a dict of KEYWORD arguments that aren ' t bound to either other Parameter. This is corresponds to a ' **kwargs ' parameter in a Python function definition.

This parameter is the two asterisk "* *" parameter, meaning that the above one of the single star parameters can accept the position parameter, change into only the key-value pairs in the way that keyword, and placed in the dictionary provided to the function:

def foo (arg1, Arg2, **ARG3):
    print (Arg1, arg2, Arg3)

foo (' Doc ', ' txt ', a= ' png ', b= ' jpeg ') #doc txt {' A ': ' png ', ' B ' : ' JPEG '}

#同单个星号一样, if you need to pass in the dictionary.
foo (' Doc ', ' txt ', {' A ': ' png ', ' b ' = ' jpeg '}) #TypeError: foo () takes 2 positional arguments but 3 were given

#用 * try.
foo (' Doc ', ' txt ', *{' a ': ' png ', ' b ' = ' jpeg '}) #TypeError: Foo2 () takes 2 positional arguments but 4 were given

#用 * * * Try Try
foo (' Doc ', ' txt ', **{' a ': ' png ', ' b ' = ' jpeg '} ' #doc txt {' A ': ' png ', ' B ': ' JPEG '}

#综上, when you need to pass the dictionary to any number of keyword parameters, you need to use the * * Dict in the way of the incoming.

Summary: Part citation from Blogger's blog

Summary: Only the second python in the five parameter forms does not provide a defined method, while the other four are defined in a way that should be defined in terms of Python's analytic laws, where:

The position or keyword parameter (positional_or_keyword) should be at the front, where no default value should precede the parameter with a default value

Only keyword parameters (keyword_only) should be placed after any number of positional parameters (var_positional)

Any number of positional parameters (var_positional) should be placed behind all position or keyword parameters

Any number of keyword parameters (Var_keyword) should be placed behind any number of positional parameters

* The order is as follows (Positional_or_keyword, var_positional, Keyword_only, Var_keyword)

* * Note: Any number of positional parameters and any number of keyword parameters can only be defined once in the definition.

def func (arg1, arg2= ' default ', *args, **kwargs): out= "arg1=%s, arg2=%s, args=%s, kwargs=%s"% (arg1, arg2, args, Kwar GS) print (out) func (1) # # correct func (1,2) # correct func (1,2,3,4) # correct func (1,2,3,4,x=1,y=2) # # correct Fun C (1,2,x=1) # # correct func (x=1) # wrong Func (arg1=1) # # correct func (1,x=1) # # correct #output arg1=1, Arg2=default, AR Gs= (), kwargs={} arg1=1, arg2=2, args= (), kwargs={} arg1=1, arg2=2, args= (3, 4), kwargs={} arg1=1, arg2=2, args= (3, 4), KW args={' y ': 2, ' X ': 1} arg1=1, arg2=2, args= (), kwargs={' x ': 1} arg1=1, Arg2=default, args= (), kwargs={} wrong Arg1=1, arg2 =default, args= (), kwargs={' x ': 1} #而加上仅位置参数就简单了, that is, each time you must pass in the position only parameter, otherwise you will get an error: def func (arg1, arg2= ' default ', *args, Arg3, **kw args): out= "arg1=%s, arg2=%s, args=%s, arg3=%s, kwargs=%s"% (arg1, arg2, args, Arg3, Kwargs) print (out) func (1) #wrong func (1, arg3=2) #right func (1, 2, 3, 4, arg3=5, x=1,y=2) #right #output wrong arg1=1, Arg2=default, args= (), arg3=2, kwargs={} arg1=1, arg2=2, Args= (3, 4), arg3=5, kwargs={' y ': 2, ' X ': 1} 
Related Article

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.