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!
Python functions can have multiple parameters.
We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.
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.
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.
Almost. It says use name if it does not evaluate to false. Things that evaluate to false include, but are not limited to:
False
(), [], ""
){}
)None
Edit Added the link provided by SilentGhost in his comment to the answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With