What does mean

前端 未结 1 467
耶瑟儿~
耶瑟儿~ 2021-01-28 05:48

Here\'s the code:

def my_func(f, arg):
  return f(arg)

print((lambda x: 2*x*x, (5)))

>>>( at 0x10207b9d8>, 5)

1条回答
  •  一生所求
    2021-01-28 06:45

    There is no error; you simply supplied two arguments to print, the lambda x: 2*x*x and 5. You're not calling your anonymous function rather, just passing it to print.

    print will then call the objects __str__ method which returns what you see:

    >>> str(lambda x: 2*x*x)  # similarly for '5'
    ' at 0x7fd4ec5f0048>'
    

    Instead, fix your parenthesis to actually call the lambda with the 5 passed as the value for x:

    print((lambda x: 2*x*x)(5))
    

    which prints out 50.


    What's the general meaning of ?

    That's simply the way Python has chosen to represent function objects when printed. Their str takes the name of the function and the hex of the id of the function and prints it out. E.g:

    def foo(): pass
    print(foo) # 
    

    Is created by returning the string:

    "".format(foo.__name__, hex(id(foo)))
    

    0 讨论(0)
提交回复
热议问题