Python notes (15): anonymous functions and @ property

Keywords: Python Lambda

(1) Anonymous function

You can use anonymous functions when you don't want to display the defined functions.

1 def f(x):
2     return x*x
3 
4 #Assign anonymous functions to a variable
5 result = lambda x:x*x
6 
7 print(result(5))
8 print(f(5))

From the output, we can see that lambda x:x*x is actually:

1 def f(x):
2     return x*x

The lambda keyword represents an anonymous function. Through the above comparison, we can see that the x in front of the colon is the parameter of the function, and the expression in the back is the return value of the anonymous function.

There is only one expression for anonymous function, so you do not need to write return, because the result of the expression is the return value of the anonymous function.

Example 1:

1 def divisible(n):
2 
3     return n % 2 == 0
4 #Explain it here filter(function, iterable)Function, the first parameter is a function, the second is a sequence
5 #Each element of the sequence is passed as a parameter to the function for judgment, and then returns True or False,Will return True The elements of are placed in the new list.
6 the_list =list( filter(divisible,range(20)))

Use anonymous functions to rewrite the above code: the following is the same as the above

1 the_list = list(filter(lambda n: n % 2 == 0,range(20)))

Example 2:

Anonymous functions can also define multiple parameters

1 def f(x,y):
2 
3     return x+y
4 
5 l = lambda x,y: x + y
6 
7 print(f(1,3))
8 print(l(1,3))

(2) @ property

For example, there is now a class that can store human age

 1 class people():
 2 
 3     def __init__(self,age=1):
 4         self.age = age
 5 
 6 #Pass below p = people()   p.age = 100,We can set a person's age
 7 
 8 p = people()
 9 p.age = 100
10 print(p.age)

Now there is a new requirement that age can only be entered as an integer greater than 0. One solution is to hide properties. We can define getter and setter interfaces to operate properties

 1 class people():
 2 
 3     def __init__(self,age=1):
 4         self.set_age(age)
 5 
 6     #Return age
 7     def get_age(self):
 8         return self._age
 9     #Age setting
10     def set_age(self,value):
11        if  value <= 0  or not isinstance(value,int) :
12            raise ValueError('Please enter an integer greater than 0')
13        self._age = value
14 
15 p = people(20)
16 print(p.get_age())
17 p.set_age(25)
18 print(p.get_age())

The above code can implement the requirements, but a simpler way is to use @ property to implement the code

 1 class people():
 2 
 3     def __init__(self,age=1):
 4         self._age = age
 5     #On methods that return properties, use the@property Modifier
 6     @property
 7     def age(self):
 8         return self._age
 9 
10     #Use properties in methods that set properties.setter Modifier
11     @age.setter
12     def age(self,value):
13        if  value <= 0  or not isinstance(value,int) :
14            raise ValueError('Please enter an integer greater than 0')
15        self._age = value
16 
17 p = people()
18 
19 p.age = 20 #And previous p.set_age(20) It's the same.
20 print(p.age) #Get the value of the property, and before p.get_age()The effect is the same

Posted by chantown on Sat, 15 Feb 2020 08:29:52 -0800