Python function parameter Understanding

Source: Internet
Author: User

1. Position parameters

When a function is called, the parameter assignment is assigned in order of position.

e.g.

1 def function (x): 2 3 return x * x4print function (2)

Output results: 4

1 def function (x, n): 2 3 return x/ n4print function (4, 2)   

Output results: 2

2. Default parameters

When the function is defined, the value of the parameter is specified directly.

e.g.

1 def function (x, n = 2):  2  3     s = 1 4  5while      n >< c10> 0:  6  7         n = n-1 8  9         s = S * x
   
    11     
    return 
     s
    
    ,
    print function (20)
   

Output results: 400

Attention:

1. The required parameter must be before the default parameter.

2, when the function has more than one parameter, the change is placed in front of large.

Benefit: Reduce the difficulty of calling the function.

3. When there are multiple default parameters, you can assign them in order or by using the parameter name directly.

e.g.

1 defEnroll (name, gender, age = 6, City ='Beijing') :2 3     Printname4 5     PrintGender6 7     Print Age8 9     Print CityTen  OneEnroll'AAA','F', 7)

Output content:

Aaa

F

7

Beijing

1 Enroll ('bbb'M'Tianjin' )

Output content:

Bbb

M

9

Tianjin

4. Special attention

e.g.

1 def add_end (L = []):23 l.append ('end')  45return  L67 add_end ([1,2,3,4,5])   

Output content:

[1,2,3,4,5, ' END ']

When Add_end () is called more than once

1 add_end () 2 3 add_end ()

Output content:

[' End ', ' end ']

The input parameters must be judged:

1 defAdd_end (L = []):2 3     ifL isNone:4 5L = []6 7L.append ('END')8 9     returnLTen  One add_end () A  -Add_end ()

Output content:

[' END ']

5, can design the immutable object is designed to be the same object.

3. Variable parameters

The number of arguments passed in is variable.

4. Keyword parameters

5. Named keyword parameter

6. Parameter combination

Summarize:

There are two rules in the parameter passing process:

1. Copying parameters to a local scope object by reference means that the variables used to access the function arguments are independent of the object provided to the function, and that modifying the local variables does not modify the original data because there is a replication problem.

2, you can modify the variable object in the appropriate position. mutable objects are mainly lists and dict, where the proper position is to modify the local object without changing the ID of the list object or the Dict object, which is the storage location.

1 #!/usr/bin/python2 3 defmodifier (number, String, list):4Number = 55String ='GOODBYE'6List = [+/-]7     Print 'Inside:', number, string, list8 9num = 10TenString ='Goodbye' OneList = [4,5,6] A Print 'Before:', num, string, list -  - modifier (num, string, list) the  - Print 'After :', num, String, list

Output content:

Before:10 Goodbye [4, 5, 6]

Inside:5 GOODBYE [1, 2, 3]

After:10 Goodbye [4, 5, 6]

From the output results, the data is not changed before and after the data exchange, although the parameters passed in the function local area are modified accordingly, but the contents of the argument object cannot be changed. This is very similar to the C language.

Because the passed in parameters are modified inside the function, that is, the variable points to different stores (objects), which is equivalent to the real participation parameter in the C language, only the formal parameters are modified, the arguments are not changed.

This means that the parameter is internally re-assigned a value and does not change the argument object.

1 #! /usr/bin/python2 defmodifier (list, dict):3List[0] = 104dict['a'] = 105     Print 'inside:list =%s, Dict =%s'%(list, dict)6 7List = [1,2,3,4,5]8Dict = {'a': 1,'b': 2,'C': 3}9 Print 'defore:list =%s, Dict =%s'%(list, dict)Ten  One modifier (list, dict) A Print 'after:list =%s, Dict =%s'% (list, dict)

Output Result:

Defore:list = [1, 2, 3, 4, 5], dict = {' A ': 1, ' C ': 3, ' B ': 2}
Inside:, List = [2, 3, 4, 5], dict = {' A ': ten, ' C ': 3, ' B ': 2}
After:list = [2, 3, 4, 5], dict = {' A ': ten, ' C ': 3, ' B ': 2}

From the output, it can be seen that the parameters passed in the function are modified, and the actual parameter object is modified, the real modification is the data in the storage location of the list and dict, the variable point does not change, so the argument object also changed.

Python function parameter Understanding

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.