我试图查看Python对lambda的看法。
print(help(lambda))
然后我得到:
print(help(lambda))
^
SyntaxError:语法无效
为什么?
答案 0 :(得分:1)
问题是help
拿了一个对象。
即类,类实例(对象),函数...
但是lambda
都不是这些东西-它只是表示函数定义的关键字。 def
也是如此。其他关键字例如for
,in
。
如果您需要有关lambda的帮助,则无法将其传递给help
,您需要像taurus05所说的那样在help()
中进行搜索。
编辑:似乎,如果您传递字符串,则帮助会自动进行搜索。因此help('lambda')
,help('def')
和其他所有元素都可以正常工作!
答案 1 :(得分:-1)
Help函数返回与python模块,对象或方法有关的帮助(如果使用各自的参数但不带任何参数调用),它将返回与当前正在运行的编程模块有关的帮助。
如果您需要有关lambda的功能及其工作方式的帮助,则可以通过以下方式进行操作:
>>> help()
Welcome to Python 3.7's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help> lambda
Lambdas
*******
lambda_expr ::= "lambda" [parameter_list] ":" expression
lambda_expr_nocond ::= "lambda" [parameter_list] ":" expression_nocond
Lambda expressions (sometimes called lambda forms) are used to create
anonymous functions. The expression "lambda parameters: expression"
yields a function object. The unnamed object behaves like a function
object defined with:
def <lambda>(parameters):
return expression
See section Function definitions for the syntax of parameter lists.
Note that functions created with lambda expressions cannot contain
statements or annotations.
Related help topics: FUNCTIONS