Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 368
AP257 Avatar asked Nov 08 '10 14:11

AP257


People also ask

Can a function have multiple parameters Python?

Python functions can have multiple parameters.

How do you pass multiple values to a single argument in Python?

We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.

Can Python function returns multiple values?

You can return multiple values from a function in Python. To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week. Data structures in Python are used to store collections of data, which can be returned from functions.

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.


1 Answers

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.

like image 186
Björn Pollex Avatar answered Sep 30 '22 03:09

Björn Pollex