Parameters of the Python function

Source: Internet
Author: User

each language has its own specific method of defining function parameters. The Python language is very flexible in defining function parameters, and it provides three ways to define function parameters. Let's take a look at it and hope to help you learn python.

1. Positional arguments position parameter

this is The simplest way to pass parameters to a function in Python. When a function is called, the function arguments in the calling code are in the same order as the parameters of the function definition. For example:

>>> defpower (x, y): ... r = 1 ...  while y > 0: ... r = r * X ... y = y-1 ... return R ... >>> power (3, 4) 27

in the code above, the function is called 3->x,4->y. You can also specify a default value for a function parameter when defining a function, similar to the following:

def Fun (arg1, ARG2=DEFAULT2, ARG3=DEFAULT3, ...)

contains a function parameter with a default value, which must be in the position of the last face of the function argument list. If the function definition above is arg1 also contains the default values, all subsequent arguments should contain the default values.

fun (1) 1- >ARG1 arg2=default2 Span style= "font-family: the song Body;" >, arg3=default3 fun ( 1,2,3 1->arg1 , Span style= "FONT-FAMILY:CALIBRI;" >2->ARG1 3->arg3 Span style= "font-family: the song Body;" >

2. Keyword arguments keyword parameter

You can use the name of the corresponding function parameter to pass the argument when the function is called. For example , the definition of the power function above, when invoked, by the following way:

>>> Power (y=2, x=3) 9

The above function call, because the name of the function parameter is used to pass the parameter, so the position of the parameter is irrelevant.

3. variable-length argument variable quantity parameter

Python 's functions can also handle a variable number of function parameters. There are two ways of doing this:

3.1. handling any number of positional parameters

in the parameter list of the function, add a before the last parameter * number, the extra arguments passed in to the function can exist in a tuple named after the parameter. For example, the following function is defined:

>>> def maximum(*numbers): ... if len (numbers) = = 0: ... return None... else: ... maxnum = numbers[0] ...  for n in numbers[1:]: ... if n > maxnum: ... maxnum = n ... return Maxnum

Called by the following method:

>>> Maximum (3, 2, 8) 8>>> maximum (1, 5, 9,-2, 2) 9

3.2. handling any number of keyword parameters

in the parameter list of the function, add a before the last parameter ** number, the extra keyword argument passed in to the function exists in a dictionary named after the parameter. For example, the following function is defined:

>>> def example_fun(x, Y, **other):. Print ("x: {0}, Y: {1}, keys in ' other ': {2}". f Ormat (x, ... y, list (Other.keys ()))) ... other_total = 0 ... for K in Other.keys (): ... other_total = other_total + other[k] ... print ("The total of values in ' other ' is {0} ". Format (other_total))

Called by the following method:

>>> example_fun (2, y= "1", Foo=3, bar=4) x:2, y:1, Keys in ' other ': [' foo ', ' Bar ']

The total of values in ' other ' is 7

4. Mixing argument-passing techniques Mixing Parameters

you can mix the above 3 ways to define a function , as long as the variable-length function parameter is defined at the end of the function argument list. For example:

# Accept variable number of positional or keyword argumentsdef spam(*args, **kwargs):

# args is a tuple of positional args

# Kwargs is Dictionary of keyword args

...

Source: Andy ' s TechBlog

Parameters of the Python function

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.