-1

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?

Rauhotz
  • 7,914
  • 6
  • 40
  • 44
  • all the keywords work .. the problem is it must be an "expression" not a "statement" (if you feel the other question is not a duplicate comment here as to why and I will vote for a reopen if appropriate) – Joran Beasley Mar 23 '16 at 18:47
  • For example "lambda : raise Exception()" does not work, so it seems that "raise" is not a valid keyword in expressions. "print" is valid in Python 3, not Python 2. – Rauhotz Mar 23 '16 at 19:29
  • 1
    `raise Exception()` is a statement not an expression ... read the dupe – Joran Beasley Mar 23 '16 at 19:32
  • Right, so the _raise_ keyword is not a valid keyword in expressions, or can you construct an expression that contains _raise_? I just want the list of valid keywords. – Rauhotz Mar 23 '16 at 20:27
  • 1
    no raise cannot be used in an expression ... you are right about that ... but it has nothing to do with it being a keyword ... – Joran Beasley Mar 23 '16 at 23:39
  • Sorry, but this is absolutely not helpful. The question is no duplicate and should be reopened. – Rauhotz Mar 23 '16 at 23:50

1 Answers1

3

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.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272