-1

In Python (assuming a function parameter's default value is None),
is there any difference between passing in the keyword parameter
(when calling the function) as
None, and not passing it in at all?

I just want to make sure there isn't some subtle semantic difference between the two.

I am talking about this one in particular scipy.optimize.curve_fit.

and the p0 parameter.

martineau
  • 119,623
  • 25
  • 170
  • 301
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • It depends on how exactly the function is declared. – bereal Jan 11 '21 at 13:55
  • "The None keyword is used to define a null value, or no value at all." – icecube Jan 11 '21 at 13:55
  • 2
    Does this answer your question? [What is the point of passing keywords equal to \`None\` as parameters in a function?](https://stackoverflow.com/questions/54220977/what-is-the-point-of-passing-keywords-equal-to-none-as-parameters-in-a-functio) – buran Jan 11 '21 at 13:56
  • What do you mean it depends? I am talking about this one in particular https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html and the `p0` parameter. – peter.petrov Jan 11 '21 at 13:56
  • @peter.petrov - in that scipy example, take a look at [these lines in the source](https://github.com/scipy/scipy/blob/ab1c0907fe9255582397db04592d6066745018d3/scipy/optimize/minpack.py#L704-L713). the function takes different paths if certain values are `None`, but that's up to the developer. – jkr Jan 11 '21 at 13:58
  • @jakub OK, thanks... But `p0` will be `None` no matter if I pass it in as `None` or not pass it in at all, is that correct? – peter.petrov Jan 11 '21 at 14:00
  • 3
    Is your question "Does it make a difference if I pass None as argument if it already is the default value for the parameter", well, no, it doesn't. `None is None`. – Thierry Lathuille Jan 11 '21 at 14:00
  • @ThierryLathuille Yeah, I think that's a better formulation of my question. – peter.petrov Jan 11 '21 at 14:01
  • 1
    @peter.petrov - yes, `p0` will be `None` if you pass it in or not, because the default is `None` in the function signature. – jkr Jan 11 '21 at 14:07

3 Answers3

2

If you don't pass anything, the value will be changed to the default value specified. If you pass None, then it will not use the default.

def foo(bar='foobar'):
    print(bar)

foo()
foo(bar=None)

Outputs:

foobar
None
big_bad_bison
  • 1,021
  • 1
  • 7
  • 12
2

There is a very big difference:

def f(x=3):
    print(x)

def g():
    print("OK")

def h(x):
    print(x)

f() outputs 3, while f(None) outputs None.

g() outputs OK, while g(None) raises a TypeError.

h() raises a TypeError, while h(None) outputs None.


Put briefly, using None indicates semantic absence, not syntactic absence.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks. But the 2nd example is not what I have in mind. The function does have some argument e.g. `p0` and I am talking about not passing it or versus passing it in with `None` value. – peter.petrov Jan 11 '21 at 13:58
  • The second example demonstrates that passing `None` when no argument is an error. Similarly (and what I should add to the answer) is that passing no value when expecting an argument without a default value is an error. – chepner Jan 11 '21 at 14:02
  • Thanks for the details in the 3 scenarios... I think that's the explanation I needed. – peter.petrov Jan 11 '21 at 14:03
1

Let's say you need key parameter in your function ;

If you do not pass a key parameter, you won't be able to access key at all in your keyword arguments ({ KeyError`)

If you pass keyas None, the value of keyin your keyword arguments will beNone`.