Python function-parameter type __ function

Source: Internet
Author: User

1. Default value parameter

That is, the default value is set for the function parameter, when the function is called, the default value parameter is not passed, the defaults are invoked automatically, and the replaced argument is used if the parameter values for the default value are set.

You can use ' function name. Func_defaults ' (use ' function name. __defaults__ ' in Python3) to view the current values of all default value parameters for a function. The return value is a tuple that sequentially sorts the default values.


When you define a function with a default value parameter, the default value parameter must appear at the very right end of the function parameter list.


2. Key parameters

The arguments used when calling are not affected by the order.

3. Variable length parameters

*x is used to receive any number of arguments and place them in a tuple, **x to receive multiple arguments in a dictionary.

>>> def f (*x):
Print X
>>> F (1,2,3)
(1, 2, 3)


>>> def H (**x):
For I in X.items ():
Print I
>>> h (x = 1,y = 2,z = 3)
(' Y ', 2)
(' x ', 1)
(' Z ', 3)

>>> def H (**x):
Print X



>>> h (x = 1,y = 2)
{' Y ': 2, ' X ': 1}
>>> def H (**x):
Print X.keys ()
>>> h (x = 1,y = 2,z = 3)
[' Y ', ' x ', ' Z ']
>>> def H (**x):
Print x.values ()
>>> h (x = 1,y = 2,z = 3)
[2, 1, 3]

4. Sequence solution of parameter transfer

When passing arguments to functions that contain multiple variables, you can use lists, tuples, dictionaries, collections, and other iterated objects as arguments, and a *,python interpreter in front of the argument name is automatically decompressed and passed to multiple variables. If you use a dictionary as an argument, the default is a key, and the key value pair uses the items method, the value values method.

>>> def f (a,b,c):
Print A+b+c
>>> s = [1,2,3]
>>> F (*s)
6

>>> t = (1,2,3)
>>> F (*t)
6
>>> d = {1: ' A ', 2: ' B '}
>>> d = {1: ' A ', 2: ' B ', 3: ' C '}
>>> F (*d)
6
>>> s = {1,2,3}
>>> F (*s)
6


5.return

There is no return value returned statement that returns none.


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.