What Does An Equality Mean In Function Arguments In Python?
Solution 1:
In a function definition, it specifies a default value for the parameter. For example:
>>>deffunc(N=20000):...print(N)>>>func(10)
10
>>>func(N=10)
10
>>>func()
20000
In the first call, we're specifying a value for the N
parameter with a positional argument, 10
. In the second call, we're specifying a value for the N
parameter with a keyword argument, N=10
. In the third call, we aren't specifying a value at all—so it gets the default value, 20000
.
Notice that the syntax for calling a function with a keyword argument looks very similar to the syntax for defining a function with a parameter with a default value. This parallel isn't accidental, but it's important not to get confused by it. And it's even easier to confuse yourself when you get to unpacking arguments vs. variable-argument parameters, etc. In all but the simplest cases, even once you get it, and it all makes sense intuitively, it's still hard to actually get the details straight in your head. This blog post attempts to get all of the explanation down in one place. I don't think it does a great job, but it does at least have useful links to everything relevant in the documentation…
Solution 2:
It specifies a default value. This can be especially useful if the program will fail on an undefined value. For instance, if it was simply n, and you did not feed the function any variables it would fail. With the default it does not.
Post a Comment for "What Does An Equality Mean In Function Arguments In Python?"