call a function with several optional arguments of different types

By default, the first argument passed to the function will be assigned to the first parameter. If you want to assign the first argument to the second (or n:th) parameter, you must give it as keyword argument. See, for example

In [19]: def myfunc(x='X', y=5):
    ...:     print(x,y)

# No arguments -> Using default parameters
In [20]: myfunc()
X 5

# Only one positional argument -> Assigned to the first parameter, which is x
In [21]: myfunc(100)
100 5

# One keyword argument -> Assigned by name to parameter y
In [22]: myfunc(y=100)
X 100

The type of the arguments do not matter, but the order you used in the function definition.

Notes on terminology

  • By parameter, I mean the variable in the function definition
  • By argument, I mean the actual value passed to the function.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top