''' 匿名函数: lambda ''' def Add(a,b): print(a+b) Add(2,3) per = lambda a,b:a+b print(per(2,3))
'' ' Three head operation ' '' A = 20 is Print ( " True " ) IF A> 10 the else Print ( " False " )
'' ' Anonymous trinocular arithmetic function + ' '' Login the lambda = username, password: Print ( 'successful login') if username == 'shenqiang' and password == '123' else print ( ' Login failed') Login ( 'shenqiang', '123')
'' ' Anonymous functions, sorting dictionary ' '' Data = kwargs the lambda **: dict (the sorted (kwargs.items (), the lambda = Key Item: Item [0])) Print (Data (name = 'shenqiang', age = 28))
'' ' Internal function map (function), for the same elements in the list to do the same thing ' '' List1 = [1,23,4,5,6 ] Print (List (map ( the lambda X: X + 100, list1)))
'' ' Internal functions of the filter function (), the elements in the filter list ' '' List2 = [1,2,3,4,5,6 ] Print (List (filter ( the lambda A: A>. 1, List2 )))
'' ' Closed: the function of the code has been achieved as far as possible not to modify the open: code to expand the existing functionality needs: prints getInfo calling f or f1, and then print f ' '' DEF getInfo (FUNC): DEF info (): Print ( " infinite automated testing " ) FUNC () return info @getInfo DEF f (): Print ( " NetEase cloud platform " ) @getInfo DEF f1 (): Print ( " 51CTO platform " ) f () ' '' steps: 1. when we execute getInfo time to be decorated f as an argument 2.getInfo function's return value will be reassigned 3. Once the binding decorators, when calling the function f, the function is actually called info part, f1 is the original cover 4. Garnished f reassigned to the decorator's info '' ' DEF the Login (FUNC): DEF Inner (Token): IF Token == " 01293 " : return FUNC (Token) the else : Print ( " login failed " ) return Inner @login DEF Profile (Token): Print ( " Login successful " ) Profile ( " 01293 " )