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

193 Views Asked by At

enter image description here

For code completion with Scintilla.Net i need the list of keywords that are valid in an expression. You can get all keywords via the "keyword" module, but for example "raise" and "print" cannot be used in lambda expressions. How can i get the reduced keyword list?

keyword.kwlist yields

['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']

Which ones can be used in an expression?

1

There are 1 best solutions below

0
On

You're asking the wrong question here. Whether or not a lambda expression can use keywords has no bearing on what is actually valid.

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. 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.