lambda
has a keyword function in Python:
f = lambda x: x**2 + 2*x - 5
What if I want to use it as a variable name? Is there an escape sequence or another way?
You may ask why I don't use another name. This is because I'd like to use argparse
:
parser = argparse.ArgumentParser("Calculate something with a quantity commonly called lambda.")
parser.add_argument("-l","--lambda",help="Defines the quantity called lambda", type=float)
args = parser.parse_args()
print args.lambda # syntax error!
Script called with --help
option gives:
...
optional arguments
-h, --help show this help message and exit
-l LAMBDA, --lambda LAMBDA
Defines the quantity called lambda
Because of that, I would like to stay with lambda
as the variable name. Solutions may be argparse
-related as well.
argparse
provides destination functionality for arguments if the long option name is not the desired attribute name for the argument.For instance:
You can use dynamic attribute access to access that specific attribute still:
Better still, tell
argparse
to use a different attribute name:Here the
dest
argument tellsargparse
to uselambda_
as the attribute name:The help text still will show the argument as
--lambda
, of course; I setmetavar
explicitly as it otherwise would usedest
in uppercase (so with the underscore):There is an argparse-specific way of dealing with this. From the documentation:
Therefore, you should be able to write: