3

This is an example of of code from here:

What does the equality mean in the argument assignment to function? like N=20000 here? What is the difference between that and simply N as argument? import random,math

def gibbs(N=20000,thin=500):
   x=0
   y=0
   samples = []
   for i in range(N):
       for j in range(thin):
           x=random.gammavariate(3,1.0/(y*y+4))
           y=random.gauss(1.0/(x+1),1.0/math.sqrt(x+1))
       samples.append((x,y))
   return samples

smp = gibbs()
Cupitor
  • 11,007
  • 19
  • 65
  • 91
  • As a side note, calling this "assignment" can make things a little confusing, and "equality" even more so. Unfortunately, there's no better name for `=` that's any less confusing. So this is really just a warning to be careful not to read too much into the name for the symbol, not anything actually helpful… – abarnert Oct 22 '13 at 20:26
  • @abarnert, assignement was not for equality but assigning arguments to a function. – Cupitor Oct 22 '13 at 20:28
  • @Naji: I don't know what that comment means. But your question title and description both refer to "the/an equality", and the description then goes on to talk about "the argument assignment". The `=` is neither equality nor assignment here; it's something different. But there is no good word for what it is. (The [syntax](http://docs.python.org/3/reference/compound_stmts.html#function-definitions) doesn't name it, it just uses the literal `"="`, and the semantics described below don't give a name either.) – abarnert Oct 22 '13 at 21:41
  • @abarnert, I just mean that the "assignment" used above is used for assigning a parameter to a function. – Cupitor Oct 22 '13 at 22:00
  • @DavidRobinson This is not a duplicate of that Q - this one concerns default parameters, while the linked question asks about positional vs. keyword arguments. I can't seem to find a suitable duplicate for this question. – Steinar Lima Apr 11 '14 at 19:04

2 Answers2

5

In a function definition, it specifies a default value for the parameter. For example:

>>> def func(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…

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • If you're interested in what happens under the covers, you can inspect the function object (`func.__code__.co_varnames` is `('N',)`, and `func.__defaults__` is `(20000,)`, and the [`inspect`](http://docs.python.org/2/library/inspect.html) module tells you a bit about what those mean), or `ast.parse` your defining code and `ast.dump` the results. – abarnert Oct 22 '13 at 20:28
1

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.

MrSynAckSter
  • 1,681
  • 1
  • 18
  • 34