1, *args
#positional parameters, functions for calculating X2defPower (x):returnX *XP= Power (5)Print(P)#change Power (x) to power (x, N) to calculate xndefPower (x, N): s= 1 whilen >0:n= N-1s= S *xreturnSP1= Power (5, 2) P2= Power (5, 3)Print(p1)Print(p2)#Default Parameters#What are the benefits of using default parameters? The biggest benefit is the ability to reduce the difficulty of calling a function. defPower (x, n=2): S= 1 whilen >0:n= N-1s= S *xreturns#What is the keyword argument for? #It can extend the functionality of the function. For example, in the person function,#we are guaranteed to receive both the name and age parameters, but we can receive them if the caller is willing to provide more arguments. #Imagine you are doing a user registration function, in addition to the user name and age is required,#others are optional, and using keyword parameters to define this function will satisfy the requirements of registration. #The *args is a mutable parameter, and args receives a tuple#variable parameters can be passed directly: Func (1, 2, 3), or the list or tuple can be assembled first,#re-incoming via *args: Func (* (1, 2, 3));defHello (Greeting, *args):if(len (args) = =0):Print('%s!'%greeting)Else: Print('%s,%s!'% (Greeting,', '. Join (args)) Hello ('Hi') Hello ('Hi','Sarah') Hello ('Hello','Michael','Bob','Adam') Names= ('Bart','Lisa') Hello ('Hello', *names)
2, **kw
#Python's functions have a very flexible parameter pattern, which allows for simple invocation and very complex parameters to be passed. #default parameters must be used immutable objects, if it is a mutable object, the program will run with a logic error! #be aware of the syntax for defining mutable parameters and keyword parameters:#*args is a variable parameter, and args receives a tuple;#**kw is the keyword parameter, and kw receives a dict. #and how to pass in the syntax for variable and keyword arguments when calling a function:#variable parameters can be passed directly: Func (1, 2, 3), or the list or tuple can be assembled first,#re-incoming via *args: Func (* (1, 2, 3));#keyword parameters can be directly passed in: Func (A=1, b=2), you can first assemble dict,#re-incoming via **kw: func (**{' a ': 1, ' B ': 2}). #using *args and **kw is a Python idiom, but it can also be used with other parameter names, but it's best to use idioms. #The named keyword parameter is used to limit the name of the parameter that the caller can pass in, as well as providing a default value. #define a named keyword parameter do not forget to write the delimiter * If there is no mutable parameter, otherwise the definition will be the positional parameter. defPrint_scores (* *kw):Print('Name Socre') Print('..........') forName, scoreinchKw.items ():Print('%10s%d'%(name, score))Print() print_scores (Adam= bart=77, lisa=88,) Data={ 'Adam Lee': 99, 'Lisa S': 88, 'F.bart': 77}print_scores (**data)defPrint_info (Name, *, Gender, city='Beijing', age):Print('Personal Info') Print('-------------') Print('name:%s'%name)Print('gender:%s'%gender)Print('City :%s'%City )Print('age:%s'%Age )Print() Print_info ('Bob', gender='male', age=20) Print_info ('Lisa', gender='female', city='Shanghai', age=18)
Xuefeng Liao Website: Learning Python functions-function parameters (iii)