Search code examples
pythonnullkeywordnonetype

What is the point of passing keywords equal to `None` as parameters in a function?


I am trying to translate a Python Voronoi Generator to Java but I am not very familiar with keyword arguments and I am confused by the keyword argument that is written as a parameter of a function.

I've read this question, but it only covers calling arguments, not defining them.

It seems like the function is just setting a and b to None. I don't understand why that is necessary. Why not just type None instead of using parameters like that?

What is the point of using keyword parameters equal to None in function definitions?

The function:

class Arc:
    p = None
    pprev = None
    pnext = None
    e = None
    s0 = None
    s1 = None

    def __init__(self, p, a=None, b=None):
        self.p = p
        self.pprev = a
        self.pnext = b
        self.e = None
        self.s0 = None
        self.s1 = None

Solution

  • Keyword arguments can have a default value. In this case, the default value of a and b are None. This means that when these arguments aren't passed into the function, a and b are set to None.

    Example:

    def func(a=None):
        print(a)
    func()
    >>>None
    

    But if you do something like this:

    def func(a):
        print(a)
    func()
    >>>TypeError: func() missing 1 required positional argument: 'a'