Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a calling function in python contains variable equal to value?

Tags:

python

I have started to learn python, and I would like to ask you about something which I considered a little magic in this language.

I would like to note that before learning python I worked with PHP and there I haven't noticed that.

What's going on - I have noticed that some call constructors or methods in Python are in this form.

object.call(variable1 = value1, variable2 = value2)

For example, in FLask:

app.run(debug=True, threaded=True)

Is any reason for this convention? Or is there some semantical reason outgoing from the language fundamentals? I haven't seen something like that in PHP as often as in Python and because I'm really surprised. I'm really curious if there is some magic or it's only convention to read code easier.

like image 539
Petr Jirásek Avatar asked Aug 12 '13 19:08

Petr Jirásek


2 Answers

These are called keyword arguments, and they're usually used to make the call more readable.

They can also be used to pass the arguments in a different order from the declared parameters, or to skip over some default parameters but pass arguments to others, or because the function requires keyword arguments… but readability is the core reason for their existence.

Consider this:

app.run(True, False)

Do you have any idea what those two arguments mean? Even if you can guess that the only two reasonable arguments are threading and debugging flags, how can you guess which one comes first? The only way you can do it is to figure out what type app is, and check the app.run method's docstring or definition.

But here:

app.run(debug=True, threaded=False)

It's obvious what it means.


It's worth reading the FAQ What is the difference between arguments and parameters?, and the other tutorial sections near the one linked above. Then you can read the reference on Function definitions for full details on parameters and Calls for full details on arguments, and finally the inspect module documentation on kinds of parameters.

This blog post attempts to summarize everything in those references so you don't have to read your way through the whole mess. The examples at the end should also serve to show why mixing up arguments and parameters in general, keyword arguments and default parameters, argument unpacking and variable parameters, etc. will lead you astray.

like image 124
abarnert Avatar answered Oct 13 '22 14:10

abarnert


Specifying arguments by keyword often creates less risk of error than specifying arguments solely by position. Consider this function to compute loan payments:

def pmt(principal, interest, term):
    return **something**;

When one tries to compute the amortization of their house purchase, it might be invoked thus:

payment = pmt(100000, 4.2, 360)

But it is difficult to see which of those values should be associated with which parameter. Without checking the documentation, we might think it should have been:

payment = pmt(360, 4.2, 100000)

Using keyword parameters, the call becomes self-documenting:

payment = pmt(principal=100000, interest=4.2, term=360)

Additionally, keyword parameters allow you to change the order of the parameters at the call site, and everything still works correctly:

# Equivalent to previous example
payment = pmt(term=360, interest=4.2, principal=100000)

See http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments for more information.

like image 35
Robᵩ Avatar answered Oct 13 '22 13:10

Robᵩ