Advanced applications Python-- function

First, the function assigned to the variable

Functions are also objects can be assigned to variables, when the function is assigned to a variable, you can call the function through a variable.

Example:

Test DEF (X, Y): 
	return X * Y 

# the functions assigned to the variable 
A = Test 
Print (A) 
# Print <Test function AT 0x00000000030187B8> 
# variable by calling function 
Print (A (2,3)) 
# 6 Print 

# the return value is assigned to the function B 
B Test = (4,5) 
Print (B) 
# 20 Print

In the above example, we can see the difference between a = test and a = test (4,5) a, a = test variable points belonging to the function, called at this point test (2,3) and calls the variable a (2,3) completely Like; a = test (4,5) belonging to a received variable test (4,5) return value, if the test (4,5), then no return value, a = None, and performing a = test (4, 5) the process, test (4,5) is also performed once.

Second, a function parameter as a function

Look at a simple code:

def test(x,y):
	a = x + y
	return 2 * a 

When the preparation process, since x + y above code change frequently, which at this time can change frequently separated logic, we need to use this time as a function of a function parameter.

Example:

DEF subtraction (X, Y): 
	return X - Y 

DEF Addition (X, Y): 
	return X + Y 

DEF Double (n-): 
	return 2 * n- 

# The following codes are used subtraction () and addition () function returns the value as Double () parameters to dynamically change the code 
by multiplying the sum # 2 after 
Print (Double (addition (2,3))) 
# 10 Print 

# after subtraction is multiplied by 2 
Print (Double (subtraction (6,2 ))) 
# print 8

  

Third, use a function as the return value

Python is used as the return value of the function to other functions.

Example:

NUM DEF (fn): 
	DEF subtraction (X, Y): 
		return X - Y 
	DEF Addition (X, Y): 
		return X + Y 
        # fn parameter based on the value returned back to local functions 
	IF fn == 'subtraction': 
		return subtraction 
	the else: 
		return Addition 

# when calling NUM () function, the function returns a nested 
a = num ( 'subtraction') # also write Print (NUM ( 'subtraction') (5,1)) 
Print (a ( 5,2)) 
# printing. 3 

B = NUM ( 'Addition') 
Print (B (2,4)) 
# 6 Print  

The above code, defined NUM () function, wherein the function returns a function of the function of the local, the NUM () function define the subtraction () and addition () two local function, NUM () The transfer function the parameters selected one of two partial functions as the return value therein.

 

Four, lambda expressions

Using lambda expressions, can make the code more concise, lambda expressions can be called and passed in the program.

lambda expression syntax is as follows:

lambda [parameter list]: Expression

The third dot code, a lambda expression can be used to simplify the local function, for example:

# Original code 
DEF NUM (Fn): 
	DEF subtraction (X, Y): 
		return X - Y 
	DEF Addition (X, Y): 
		return X + Y 
	IF Fn == 'subtraction': 
		return subtraction 
	the else: 
		return Addition 


# simplification after the code 
DEF NUM (Fn): 
	IF Fn == 'subtraction': 
		return the lambda X, Y: X - Y 
	the else: 
		return the lambda X, Y: X + Y 

Print (NUM ( 'subtraction') (5,2) ) 
# Print 3 
Print (NUM ( "abc") (5,2)) 
# print 7

Simplified code above, using the keyword lambda lambda expression is defined, to note here is, can only be a single-line lambda expression expression, and must use the lambda keyword to define. lambda expression will be released immediately after use and improve performance.

Guess you like

Origin www.cnblogs.com/mingmingming/p/11084042.html