0

I have defined a function in Python which takes some arguments and plots a graph. I would like to extend this so that if certain additional optional arguments are passed into the function then it will plot another graph on the same set of axes but only if these optional arguments are passed into the function. The plotting of the data inside the function is not the issue but how would I create a function that will work if optional arguments are not passed?

For example, I would like to define a function such as below which will work even if optional_arg3 and optional_arg4 are not passed.

def function(arg1, arg2, optional_arg3, optional_arg4):

Any help would be really appreciated!

Thomas
  • 23
  • 4
  • You could specify a default value for them and that is pretty much it – Anshumaan Mishra Mar 25 '22 at 14:36
  • ... aside from every single Python tutorial explaining this very early, this could have been found with the same keywords as in your title on this site. Maybe you really want to go back and review the introduction to Python you've read? That's probably more time-efficient than having to ask about central language constructs! – Marcus Müller Mar 25 '22 at 14:38
  • 1
    Sadly, most of the answers in the proposed duplicate never really get to the point, which is that parameters with default values, a `*` parameter, and a `**` parameter are all ways of accepting optional arguments. – chepner Mar 25 '22 at 14:40
  • Do a search for positional arguments and named arguments in Python – Mark R Mar 25 '22 at 14:42
  • One could make a distinction between *optional arguments* (those that would be assigned to parameters expecting a value) and *accepted arguments* (those assigned to `*` and `**` parameters). – chepner Mar 25 '22 at 14:44

1 Answers1

1

This is easy in python by providing default values for arguments. So if no value is passed, the arguments take up the default value. Eg:

def a(b, c, d=None):
    print(b, c, d)
    
a(1,2)

since I only passed b and c, d takes up the default value of None
So output:

1 2 None
Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19