What is a lambda (function)?

后端 未结 23 2794
太阳男子
太阳男子 2020-11-22 04:47

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

23条回答
  •  猫巷女王i
    2020-11-22 05:19

    For a person without a comp-sci background, what is a lambda in the world of Computer Science?

    I will illustrate it intuitively step by step in simple and readable python codes.

    In short, a lambda is just an anonymous and inline function.

    Let's start from assignment to understand lambdas as a freshman with background of basic arithmetic.

    The blueprint of assignment is 'the name = value', see:

    In [1]: x = 1
       ...: y = 'value'
    In [2]: x
    Out[2]: 1
    In [3]: y
    Out[3]: 'value'
    

    'x', 'y' are names and 1, 'value' are values. Try a function in mathematics

    In [4]: m = n**2 + 2*n + 1
    NameError: name 'n' is not defined
    

    Error reports,
    you cannot write a mathematic directly as code,'n' should be defined or be assigned to a value.

    In [8]: n = 3.14
    In [9]: m = n**2 + 2*n + 1
    In [10]: m
    Out[10]: 17.1396
    

    It works now,what if you insist on combining the two seperarte lines to one. There comes lambda

    In [13]: j = lambda i: i**2 + 2*i + 1
    In [14]: j
    Out[14]: >
    

    No errors reported.

    This is a glance at lambda, it enables you to write a function in a single line as you do in mathematic into the computer directly.

    We will see it later.

    Let's continue on digging deeper on 'assignment'.

    As illustrated above, the equals symbol = works for simple data(1 and 'value') type and simple expression(n**2 + 2*n + 1).

    Try this:

    In [15]: x = print('This is a x')
    This is a x
    In [16]: x
    In [17]: x = input('Enter a x: ')
    Enter a x: x
    

    It works for simple statements,there's 11 types of them in python 7. Simple statements — Python 3.6.3 documentation

    How about compound statement,

    In [18]: m = n**2 + 2*n + 1 if n > 0
    SyntaxError: invalid syntax
    #or
    In [19]: m = n**2 + 2*n + 1, if n > 0
    SyntaxError: invalid syntax
    

    There comes def enable it working

    In [23]: def m(n):
        ...:     if n > 0:
        ...:         return n**2 + 2*n + 1
        ...:
    In [24]: m(2)
    Out[24]: 9
    

    Tada, analyse it, 'm' is name, 'n**2 + 2*n + 1' is value.: is a variant of '='.
    Find it, if just for understanding, everything starts from assignment and everything is assignment.

    Now return to lambda, we have a function named 'm'

    Try:

    In [28]: m = m(3)
    In [29]: m
    Out[29]: 16
    

    There are two names of 'm' here, function m already has a name, duplicated.

    It's formatting like:

    In [27]: m = def m(n):
        ...:         if n > 0:
        ...:             return n**2 + 2*n + 1
        SyntaxError: invalid syntax
    

    It's not a smart strategy, so error reports

    We have to delete one of them,set a function without a name.

    m = lambda n:n**2 + 2*n + 1
    

    It's called 'anonymous function'

    In conclusion,

    1. lambda in an inline function which enable you to write a function in one straight line as does in mathematics
    2. lambda is anonymous

    Hope, this helps.

提交回复
热议问题