Python3 anonymous function usage example

  • 2020-11-20 06:09:21
  • OfStack

This article illustrates the use of Python3 anonymous functions. To share for your reference, the details are as follows:


# -*- coding:utf-8 -*-
#!python3
#  Anonymous functions 
# 1. You don't have to name the function 
# 2. Easy to use, define and use at any time 
def calc_x(x):
  return x*x
rs = calc_x(10)
print (rs)
# lambda  Argument list: Simple logical code for function execution 
# lambda  return 1 A function object that can be received using variables 
a=lambda x:x*x
#  Calling anonymous functions 
#  The variables (arguments) of the receiving function object 
s = a(10)
print (s)
#  Ordinary function, written with two arguments 
def add(x,y):
  return x+y
rs = add(10,20)
print (rs)
#  Anonymous function with two arguments 
b = lambda x,y:x+y
rs = b(10,20)
print (rs)
#  Calculate storage 100 Each of a list of Numbers 1 The square of the number, return 1 A new list 
list1 = map(lambda x:x*x, [x for x in range(100)])
list1 = list(list1)
print (list1)
print (list(map(lambda x:x*x, [x for x in range(100)])))
'''
   Anonymous function features: 
  1. You don't have to name it, because naming a function can be tricky 1 One thing, especially when there are a lot of functions 
  2. It can be defined directly in the place where it is used. If it needs to be modified, it can be found directly to modify, so as to facilitate the maintenance work of the code in the future 
  3. The syntax structure is simple and does not need to be used def  Function name (parameter name) : Defined in this way and used directly lambda  Parameter: Return value   Can be defined 
'''

Operation results:

[

100
100
30
30
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]

]

For more information about Python, please refer to Python Functions, Python Math Skills, Python Data Structures and Algorithms, Python String Manipulation skills, and Python Introductory and Advanced Classics.

I hope this article has been helpful in Python programming.


Related articles: