Have you tried using keyword arguments? I find them easy to read
def many_arg_func(
required_arg: str, # No default, so must supply value
arg1: int = 42, # Has a default, you can skip
mebbe_a_string: str = None, # Can skip this even if not a str
a_float: float = 1.234,
a_tuple: tuple = (),
even_a_dict: dict = {}):
print(arg1)
print(mebbe_a_string)
print(a_float)
print(a_tuple)
print(even_a_dict)
print('many_arg_func')
many_arg_func(
required_arg = 'This is required',
a_tuple = (1, 2, 3, 4),
even_a_dict = {'key': 'value', 'another key': 'another value'})
def func_with_optional_args(
req: str,
opt1: int = None, # Will provide default later
opt2: float = None,
opt3: tuple = None):
# Provide some defaults
if opt1 is None: opt1 = 42
if opt2 is None: opt2 = 1.234
if opt3 is None: opt3 = (1, 2, 3, 4)
print(req)
print(opt1)
print(opt2)
print(opt3)
print('\nfunc_with_optional_args')
func_with_optional_args(
req = 'required arg',
opt3 = ('one', 'two', 'three', 'four'))
def many_arg_func_helper(
# I fill in some of the args and call many_arg_func
arg1: int = None,
a_float: float = None):
many_arg_func(
required_arg = 'Required',
arg1 = arg1,
a_float = a_float,
a_tuple = ('something',),
even_a_dict = {'arg1': arg1, 'a_float': a_float})
print('\nmany_arg_func with helper')
many_arg_func_helper()
This is the output when I run the script.
Output:
many_arg_func
42
None
1.234
(1, 2, 3, 4)
{'key': 'value', 'another key': 'another value'}
func_with_optional_args
required arg
42
1.234
('one', 'two', 'three', 'four')
many_arg_func with helper
None
None
None
('something',)
{'arg1': None, 'a_float': None}
Notice the output when the helper function is used. arg_1 and a_float are both None even though the many_arg_func provides a default integer and float value. Why are they None? The reason is that default values in the argument list are only used when no value is provided. When many_arg_func_helper is called it provides arg_1 and a_float with a default value of None. When many_arg_func_helper calls "many_arg_func the None values are passed along. For this reason I only use None for default values and provide default values inside the function body.