python--built-in Type--function--parameter __ function

Source: Internet
Author: User
Tags function definition
Required Parameters
def enroll (name, gender):
    print ' name: ', Name, ' Gender: ', Gender

enroll (' Bob ', ' M ')
enroll (' Lily ', ' F ')
Output
Name:bob gender:m
name:lily gender:f
Default Parameters
def enroll (name, gender, age = 8, city = ' Hangzhou '):
    print ' name: ', Name, ' Gender: ', Gender, ' age: ', CI Ty

enroll (' Bob ', ' m ')
enroll (' Lily ', ' F ', 9)
enroll (' Jack ', ' m ', city = ' Shanghai ')
Output
Name:bob gender:m age:8 City:hangzhou
name:lily gender:f age:9 city:hangzhou name:jack gender:m age:8
City:shanghai
Summary: Before the required parameters are selected, the default parameter has more than one parameter in the following function. Change the big parameter to put the front, the change small parameter to put behind, the change small parameter can be set as the default parameter has multiple default parameters, you can supply the default parameters in order, or not in order to provide partial default parameters, when not in order to provide partial default parameters, The name of the parameter must be described
def add_end (list=[]):
    list.append (' End ') return
    list
	
print add_end ()
print add_end ()
print add_ End ()
Output
[' End ']
[' End ', ' End ']
["End", "End", "End"]
Explanation: The function is instance, when the function is defined, the default argument is determined, when the function is invoked, if the default argument is changed to the object value, the default parameter is no longer the initial value of the function definition, so the default argument must point to the immutable object, otherwise the run will have a logical error variable Parameters
def calsum (*args):
    sum = 0
    for num in args:
        sum = sum + num return
    sum
	
print calsum (), Calsum (1), C Alsum (1, 9), Calsum (1, 9,)
odd_list = [1, 3, 5, 7, 9]
even_tuple = (2, 4, 6, 8, ten)
color_dict = {1: ' Red ', 2: ' Green ', 3: ' Blue '}
prime_set = ([2, 3, 5, 7])
print calsum (*odd_list), Calsum (*even_tuple), Calsum (*color _dict), Calsum (*prime_set)
Output
0 1
25 30 6 17
Summary: The definition of the variable name variable parameters is essentially assembled into a tuple to the function of the internal deformation parameter list,tuple,dict,set, such as container types, as the argument to the deformable parameters, the front Plus * can be converted to tuple to the deformable parameters, Dict and set are key assembled into tuple keyword Parameters
def enroll (name, age, **kw):
    print ' name: ', Name, ' Age: ', age, ' other: ', kw
	
enroll (' Bob ', @)
enroll (' Jack ', City = ' Hangzhou ')
enroll (' Martin ', gender = ' M ', job = ' engineer ') dict = "City
": ' Shanghai ', ' job ': ' Do ctor '}
Enroll (' Tom ', **dict)
Output
Name:bob age:10 Other: {} Name:jack age:20, {' City
': ' Hangzhou '}
name:martin age:30, other: {' Gender ': ' M ', ' job ': ' Engineer '}
name:tom age:40: {' City ': ' Shanghai ', ' job ': ' Doctor '}
Summary: Define keyword parameters with * * Variable name keyword arguments are essentially assembled into dict to pass to the key glyph parameters inside the function dict as arguments to the key glyph parameters, the front plus * can parameter Application
Def Mixarg (A, b, c = 0, *args, **kw):
    print ' A = ', A, ' B = ', B, ' C = ', C, ' args = ', args, ' kw = ', kw
Mixarg (1, 2)  C2/>mixarg (1, 2, c = 3)
Mixarg (1, 2, 3, ' A ', ' B ')
Mixarg (1, 2, 3, ' A ', ' B ', x =)
args = (1, 2, 3, 4)
kw = {' X ': Mixarg}
(*args, **kw)
Output
A = 1 b = 2 c = 0 args = () kw = {}
a = 1 b = 2 c = 3 args = () kw = {}
a = 1 b = 2 c = 3 args = (' A ', ' b ') kw = {
A = 1 b = 2 c = 3 args = (' A ', ' b ') kw = {' X ': n '
a = 1 b = 2 c = 3 args = (4,) kw = {' X ': 98}
Summary: Required parameters, default parameters, variable parameters, keyword parameters can be used in any combination, but the order of parameter definitions must be: When required, default, variable, or keyword parameter function calls, the interpreter automatically passes the corresponding parameter to any function by parameter position and parameter name, and can be passed through a similar mixarg (* args, **kw) Form invocation, regardless of the parameter is how to define the default parameters must be used with mutable objects, if it is a mutable object, the operation will have a logical error *args is a variable parameter, args receive a tuple, variable parameters can be directly passed, can also be assembled first list,tuple, Dict,set and other container types, and then through the *args incoming **kw is the keyword parameter, KW received is a dict, keyword parameters can be directly passed in, can also be assembled dict, and then through the **kw into the *args and **kw is Python idioms, Of course, there are other parameter names, but it's best to use custom notation

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.