19

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.

2
  • 1
    Name it Lambda instead of lambda? Commented Aug 4, 2014 at 12:21
  • ...or name it _lambda or lambda_. Commented Aug 4, 2014 at 13:34

3 Answers 3

33

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 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
Sign up to request clarification or add additional context in comments.

4 Comments

dest solution does not work for positional arguments.
@unndreay: that's because for positional arguments the name is the dest parameter. Use metavar to provide a string to use in the help text there. parser.add_argument('lambda_', metavar='lambda').
Sorry, didn't want to complain. Just noted.
I didn't take it as complaining. :-)
3

There is an argparse-specific way of dealing with this. From the documentation:

If you prefer to have dict-like view of the attributes, you can use the standard Python idiom, vars().

Therefore, you should be able to write:

print vars(args)["lambda"]  # No keyword used, no syntax error.

Comments

2

argparse provides destination functionality for arguments if the long option name is not the desired attribute name for the argument.

For instance:

parser = argparse.ArgumentParser()
parser.add_argument("--lambda", dest="function")

args = parser.parse_args()

print(args.function)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.