How do I create a Python function with optional arguments?

732,252

Solution 1

Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.

Solution 2

Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.

def myfunc(a,b, *args, **kwargs):
   for ar in args:
      print ar
myfunc(a,b,c,d,e,f)

And it will print values of c,d,e,f


Similarly you could use the kwargs argument and then you could name your parameters.

def myfunc(a,b, *args, **kwargs):
      c = kwargs.get('c', None)
      d = kwargs.get('d', None)
      #etc
myfunc(a,b, c='nick', d='dog', ...)

And then kwargs would have a dictionary of all the parameters that are key valued after a,b

Solution 3

It is very easy just do this

def foo(a = None):
       print(a)

Instead of None you can type anything that should be in place if there was no argument for example if you will not write value of the parameter like this foo() then it will print None because no argument is given and if you will GIVE it a argument like foo("hello world") then it will print hello world, OH YES i forgot to tell y'all that when these types of paramters i.e optional parameters, THESE ARGUMENTS NEEDS TO FOLLOW THE DEFAULT ARGUMENTS this means that, lets take the previous function and add another parameter b

def foo(a = None, b): 
    print(a)

Now if you'll execute your python file it is going to raise an exception saying that Non-default arguments follows default arguments,

SyntaxError: non-default argument follows default argument

so you gotta put the optional or non-default argument after the arguments which are required

which means

def foo (a, b=None): ... #This one is right(these threee dots are ellipsis which are somewhat like pass

def foo(b=None, a): ... #and this isn't

Solution 4

as in more proper way:

import Optional from typing

def foo(a: str, b: Optional[str]) -> str or None:
    pass

which is equalevent to:

def foo(a: str, b: str or None = None) -> str or None
    pass

For more information: https://docs.python.org/3/library/typing.html#typing.Optional

Solution 5

Required parameters first, optional parameters after. Optional parameters always with a =None.

Easy and fast example:

def example_function(param1, param2, param3=None, param4=None):
    pass

# Doesn't work, param2 missing
example_function("hello")

# Works
example_function("hello", "bye")

# Works. Both the same
example_function("hello", "bye", "hey")
example_function("hello", "bye", param3="hey")

# Works. Both the same
example_function("hello", "bye", "hey", "foo")
example_function("hello", "bye", param3="hey", param4="foo")
Share:
732,252
Thalia
Author by

Thalia

Junior developer exploring image processing and driver development

Updated on July 08, 2022

Comments

  • Thalia
    Thalia almost 2 years

    I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios.

    def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None):
        #code
    

    The arguments d through h are strings which each have different meanings. It is important that I can choose which optional parameters to pass in any combination. For example, (a, b, C, d, e), or (a, b, C, g, h), or (a, b, C, d, e, f, or all of them (these are my choices).

    It would be great if I could overload the function - but I read that Python does not support overloading. I tried to insert some of the required int arguments in the list - and got an argument mismatch error.

    Right now I am sending empty strings in place of the first few missing arguments as placeholders. I would like to be able to call a function just using actual values.

    Is there any way to do this? Could I pass a list instead of the argument list?

    Right now the prototype using ctypes looks something like:

    _fdll.some_function.argtypes = [c_void_p, c_char_p, c_int, c_char_p, c_char_p, c_char_p, c_char_p, c_char_p]