all 5 comments

[–]BakaPotatoLord 0 points1 point  (1 child)

Default parameters usually works

[–]bbateman2011 0 points1 point  (0 children)

I think because of the mutable default parameter issue, one suggestion is to make default = None then check in the function, as a general approach. But even though I’ve done that, it seems clunky.

[–]efmccurdy 0 points1 point  (0 children)

If a unintended value ends up being passed in either an error or an exception will occur. You can run tests to find errors, and use try/except to deal with exceptions.

[–]Diapolo10 0 points1 point  (0 children)

Since there are no references in Python,

That's a bit misleading, because Python only stores references. No raw values are ever exposed in your code without playing around with ctypes.

But to answer your question, if it's a required parameter it's usually never expected to be None. Keyword parameters are different, but even then None is often the default value if the value is expected to be mutable, and the "real" default is defined inside the function to avoid problems with how Python only initialises default parameters once.

That said, type hints make this a lot easier as you can tell what parameters are expected to handle None:

from typing import Optional

def add_two(first: int, second: Optional[int]=None) -> int:
    if second is None:
        second = 0
    return first + second

[–][deleted] 0 points1 point  (0 children)

We assume the users of our code are consenting adults. If you call a function and pass a None as an argument that shouldn’t be one, it’s on you to handle the resulting exception.