When Python defines a function, the delivery of different parameter types

Source: Internet
Author: User
Tags pow

The bottom of Python is implemented through the C language, so parameters are called by address (as in C, values are one-way, pointers can be passed in two directions), such as:

#-*-Coding:utf-8-*-
def f1 (x, y):
z=x+y
y=x
return Z
a 1 b=2c=F1 (A, B) print C, a, b

The result is

3 1 2

From here we can see that the B value has not changed, that is to say, the value is one-way pass.

When defining a function, a parameter can be a numeric value, a list (when tuples are processed), a dictionary, and so on, whose parameters are defined as follows:

Def f (A, *pargs, * *Kargs):     print (A, pargs, Kargs)

In fact, this is a very important feature of Python, that is, arbitrary arguments. When you need a function to support any number of arguments, you will use it. In the example above, Python will treat the matching parameters as a tuple (Ganso). You can call function f like this,

F (13'abc', ('lls'POW ' ' A ')

Print out the results as follows:

(1, (3'abc', ('lls'POW  ')), {'param1'A'})

Python will assign 1 to the parameter A, 3, ' abc ', (' lls ', ' pow ') to pargs, and you can see that Pargs is assigned with a tuple (3, ' abc ', (' lls ', ' pow ')).
Similarly, a kargs that appears in front of the two asterisk will be matched by a dictionary, which means that all incoming parameters like param = X are transferred to the Dict assignment to Kargs.

It is important to note that when defining a function **kargs This type of argument must be placed at the end (if any), and *pargs This type of argument must precede the type parameter **kargs the other type parameter.
For example, the following definitions are illegal.

Def f (A, *b, c=6, * *D)    :**b, *c):    passdef F (*a, * *B, c    ):*c, * *D, E) :    Pass

And the following definitions are all legal.

Def f (A, * *b)    :*b):    passdef F (*a, * *b):    passsdef F (*a):    Passdef F (A, b=1, *c, * *D):    pass

To give a practical example, write a function, enter any number of numbers as parameters, find the largest one, you can use the *pargs we mentioned, the code is as follows:

1def MAX (*pargs):2Maxnum = pargs[0]3      forIinchpargs[1:]:4         ifi >Maxnum:5Maxnum =I6     returnMaxnum7 8Print Max (5,3,1, +, Wu, at,1,2, +)

Results Print 54.

Reference post: http://blog.chinaunix.net/uid-29466937-id-5752171.html

When Python defines a function, the delivery of different parameter types

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.