The most important functional programming is to enhance reusability and readability of the code
1 def function name (parameter): 2 3 ... 4 function body 5 ...
Defined functions have the following main points:
- def: function representation keywords
- Function name: name of the function, later calling the function according to the function name
- Body function: function series of logic calculation, such as: send a message, to calculate the maximum number of [11,22,38,888,2] ... the like
- Parameters: providing data as a function of the body
- Return value: When the function is finished, you can return data to the caller.
In the above points, the more important parameters and return values:
1, the return value
Function is a function block, this function is executed successfully or not in the end, it is necessary to inform the caller by the return value.
def send text messages (): send SMS code ... IF sent successfully: return True the else: return False the while True: # send text messages each time the function will return the value is automatically assigned to the result after #, can be written according to the result log, and the like, or a retransmission operation result = send SMS () IF == False Result: log message delivery failure ...
2. Parameters
Function in three different parameters:
- General parameters
- The default parameters
- Dynamic parameters
General parameters:
Common parameters are not passed and the number of data type restrictions, can pass a string, a number, and a list of dictionary. The number is not limited, it is noted that: a function of how much the parameters, the call will pass past time order and it defines the type of data.
1 def argtest(name,age,fruit_lst,hobby_dic): 2 print 'Hello,me name is %s,i\'m %d year\'s old'%(name,age) 3 print 'My favorite fruits are : %s'%'.'.join(fruit_lst) 4 print 'My hobbies:' 5 for hobby in hobby_dic: 6 print '\t%s'%hobby_dic[hobby] 7 8 lst = ['apple','banana','watermelon'] 9 dic = {'hobby one':'teaism','hobby two':'sing'} 10 argtest('Eva_J',18,lst,dic)
输出的内容:
Hello,me name is Eva_J,i'm 18 year's old
My favorite fruits are : apple.banana.watermelon
My hobbies:
teaism
sing
Default Parameters:
The default parameter is to add a parameter default values, but when we call the function, if you pass this parameter, using the values we pass over; if you do not pass the default value is used, the default parameters can have more months, but it must be last of all parameters. When we have more than one default parameters, call the function when you can use a parameter to specify the name of one of the parameters to ensure accurate parameters passed to the called function.
1 def argvtest(argv1,argv2 = 'aaa',argv3 = 'bbb'): 2 print 'argv1:',argv1 3 print 'argv2:',argv2 4 print 'argv3:',argv3 5 6 argvtest('a1',argv3 = 'a2')
输出的内容:
argv1: a1
argv2: aaa
argv3: a2
Dynamic parameters:
DEF FUNC (* args) receiving a plurality of parameters, internal automatic configuration tuples, * presequence plus avoid internal structure tuple
def func (** kwargs) receiving a plurality of parameters, automatic internal structure dictionary, plus presequence **, passed directly dictionary
def func (* args, ** kwargs ): receiving a plurality of parameters, may be automatically configured tuples, and dictionaries can be automatically constructed.
The following figure shows from left to right tuples automatic configuration, automatic configuration and dictionaries of the first two parameters of the integrated transmission mode: