Python: How to escape 'lambda'
Question: `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`](https://docs.python.org/2/library/argparse.html):

    
    
    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.


Answer: You can use dynamic attribute access to access that specific attribute still:

    
    
    print getattr(args, 'lambda')
    

Better still, tell `argparse` to use a different attribute name:

    
    
    parser.add_argument("-l", "--lambda",
        help="Defines the quantity called lambda",
        type=float, dest='lambda_', metavar='LAMBDA')
    

Here the [`dest`
argument](https://docs.python.org/2/library/argparse.html#dest) tells
`argparse` to use `lambda_` as the attribute name:

    
    
    print args.lambda_
    

The help text still will show the argument as `--lambda`, of course; I set
`metavar` explicitly as it otherwise would use `dest` in uppercase (so with
the underscore):

    
    
    >>> import 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, dest='lambda_', metavar='LAMBDA')
    _StoreAction(option_strings=['-l', '--lambda'], dest='lambda_', nargs=None, const=None, default=None, type=<type 'float'>, choices=None, help='Defines the quantity called lambda', metavar='LAMBDA')
    >>> parser.print_help()
    usage: Calculate something with a quantity commonly called lambda.
           [-h] [-l LAMBDA]
    
    optional arguments:
      -h, --help            show this help message and exit
      -l LAMBDA, --lambda LAMBDA
                            Defines the quantity called lambda
    >>> args = parser.parse_args(['--lambda', '4.2'])
    >>> args.lambda_
    4.2
    

