justincf
justincf

Reputation: 133

python one-line if statement calling function if true

I'm using argparse.

def help():
  parser.print_help()
  sys.exit(0)

help() if (args.lock and args.unlock)

This gives me a syntax error. What is wrong with my if statement?

Upvotes: 12

Views: 9267

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122342

You are using a conditional expression: true_result if condition else false_result. A conditional expression requires an else part, because it has to produce a value; i.e. when the condition is false, there has to be an expression to produce the result in that case.

Don't use a conditional expression when all you want is a proper if statement:

if args.lock and args.unlock: help() 

Upvotes: 15

Related Questions