Search code examples
pythonboolean-expression

Python: multiple possible values for function arguments


I've inherited some Python code that looks like this:

name = 'London'
code = '0.1'
notes = 'Capital of England'
ev = model.City(key=key, code=code, name=name or code, notes=notes)

In the spirit of learning, I'd like to know what's going on with the name or code argument. Is this saying 'Use name if it's not null, otherwise use code'?

And what is the technical term for supplying multiple possible arguments like this, so I can read up on it in the Python docs?

Thanks!


Solution

  • Almost. It says use name if it does not evaluate to false. Things that evaluate to false include, but are not limited to:

    • False
    • empty sequences ((), [], "")
    • empty mappings ({})
    • 0
    • None

    Edit Added the link provided by SilentGhost in his comment to the answer.