python-Function (iii)

Source: Internet
Author: User

Functions in Python (iii)

Some of the related uses of functions have been explored in the previous two articles, and here's a look at the problem of function parameter types. In C, when calling a function, it is necessary to pass the parameter in accordance with the number of arguments and the type of the function definition, otherwise there will be an error, which is strictly prescribed. In Python, however, function parameters are much more flexible than the way they are defined and passed.

I. Types of function parameters
  the kind of function parameter definition and transfer method we contacted before is called positional parameter, that is, the parameter is matched by position, from left to right, and then in order, this has strict requirement to the position and number of parameters. in Python, it is matched by a parameter name, so that you do not need to pass the parameter exactly where the parameter is defined, which is called the keyword parameter.
Here are two examples:

def display (b):    print a    print B    display ('hello','  World')  // This program is wanted to output ' Hello World ', which can run normally. 

If you write as follows, the result may not be what you expect it to look like:

def display (b):    print a    print B    display ('hello')      // This will error      display ('World','hello' )     //This will output ' world Hello '


As you can see, the default in Python is to use positional parameters to pass arguments. So the calling function must be in strict accordance with the function definition of the number of arguments and location to pass parameters, otherwise there will be unexpected results.
The following code uses the keyword parameter:

def display (b):    print a    print B//The following 2 sentences achieve the same display (a= ' World') , b='hello') display (b='hello', a= '  World ')   // you can see that the parameter position has no effect on the result when the parameter is passed by specifying the parameter name. 


The most powerful thing about keyword parameters is that it provides default values for function parameters. Like what:

 def display (A= " hello  " , B= " wolrd   " ): Print a  + Bdisplay () display (b  = " world   " ) display (a  =  hello   "  Display (  " world  " ) 

In the above code, A and B are given the default parameters, i.e., if you do not pass arguments to a or B, they will take the default values respectively. After assigning a default value to the parameter, if the parameter name is not specified when the argument is passed, the argument is passed from left to right, for example, if display (' World ') does not specify ' world ' to be passed to a or B, then the default is a left-to-right match.

While it is convenient to use the default parameters, it is important to note that when a function is repeatedly called, the default shape delegate inherits the value of the parameter after the previous call. Let's look at an example:

def insert (a,l=[]):    l.append (a)    print Linsert ('hello') Insert (  'World')   

The result of this operation is:

when a function is called repeatedly, the default shape participant inherits the value of the parameter after the call ends.




Two. Any number of parameters
In general, when we define the function, the number of function parameters is determined, but in some cases we can not determine the number of parameters, such as to store a person's name and its nickname, some people may have 2 or more nicknames, there is no way to determine the number of parameters, you can use the collection parameters, To use the collect parameters, simply precede the parameters with ' * ' or ' * * '.

Example 1,

def storename (name,*nickname): Print'real name is%s'%name forNicknameinchnickname:print Nicknamestorename ('Jack') storename (U'James', u'Little Emperor') storename (U'o ' Neill', u'Big Shark', u'Three non-stick')

// ' * ' and ' * * ' indicate the ability to accept 0 to any number of arguments, ' * ' means that no matching values are placed in the same tuple.

Example 2,

def printvalue (a,**D):    'a=%d' %a    for in d:        print x+'=%d' %d[x]printvalue (1, b=2 , c=3)

' * ' and ' * * ' indicate the ability to accept 0 to any number of arguments, ' * * ' means that no matching values are placed in a dictionary.


Add that a function in Python can return multiple values, and if multiple values are returned, multiple values are placed in a tuple or other type of collection to return.

def function ():    x=2    y=[3,4]    return x, y print function ()   

  




python-Function (iii)

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.