简体   繁体   English

在(lambda)表达式内有效的Python关键字列表

[英]List of Python keywords that are valid within (lambda) expression

在此处输入图片说明

For code completion with Scintilla.Net i need the list of keywords that are valid in an expression. 为了用Scintilla.Net完成代码,我需要在表达式中有效的关键字列表。 You can get all keywords via the "keyword" module, but for example "raise" and "print" cannot be used in lambda expressions. 您可以通过“关键字”模块获取所有关键字,但是例如在lambda表达式中不能使用“ raise”和“ print”。 How can i get the reduced keyword list? 如何获得简化的关键字列表?

keyword.kwlist yields keyword.kwlist收益

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] [“ False”,“ None”,“ True”,“ and”,“ as”,“ assert”,“ break”,“ class”,“ continue”,“ def”,“ del”,“ elif”,“ else','except','finally','for','from','global','if','import','in','is','lambda','nonlocal','not' ,“或”,“通过”,“提高”,“返回”,“尝试”,“同时”,“有”,“收益”]

Which ones can be used in an expression? 表达式中可以使用哪些?

You're asking the wrong question here. 您在这里问错了问题。 Whether or not a lambda expression can use keywords has no bearing on what is actually valid. Lambda表达式是否可以使用关键字与实际有效的内容无关。

https://docs.python.org/2.7/reference/expressions.html#lambda https://docs.python.org/2.7/reference/expressions.html#lambda

lambda_expr     ::=  "lambda" [parameter_list]: expression
old_lambda_expr ::=  "lambda" [parameter_list]: old_expression

All that matters is that the body of a lambda is an expression. 重要的是,lambda的主体是一种表达。 Or in other words, something that can be evaluated to a value. 换句话说,就是可以评估为值的东西。 That means it cannot contain statements. 这意味着它不能包含语句。

So figure out what combination of keywords can be used to form an expression and you'll have your answer. 因此,弄清楚可以使用哪种关键字组合来构成表达式,您将得到答案。

The documentation also mentions another block of code that is similar: 该文档还提到了另一个类似的代码块:

def name(arguments):
    return expression

Put another way, the body of a lambda has to be something that could be placed in a return statement... an expression. 换句话说,lambda的主体必须是可以放置在return语句中的东西……一个表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM