Python Forum
Function with many arguments, with some default values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function with many arguments, with some default values
#1
I want to create a function that takes multiple arguments (about 20).
However some of them might default to some pre-selected values.

I was thinking of organizing the arguments into dictionaries to make the code more readable, but how can I get a function with a dictionary as input to default to a value when the respective dictionary entry is not passed?
Reply
#2
Why do you have a function that needs so many things? That's a sign that it's probably doing too much.
Reply
#3
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.
Reply
#4
Think this works for me, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What data types can I use for default values? Mark17 1 522 Oct-09-2023, 02:07 PM
Last Post: buran
  calling external function with arguments Wimpy_Wellington 7 1,425 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,634 Jan-24-2023, 08:22 AM
Last Post: perfringo
  function accepts infinite parameters and returns a graph with those values edencthompson 0 855 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  'namespace' shorthand for function arguments? shadowphile 5 2,584 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,152 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Function - Return multiple values tester_V 10 4,436 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Possible to dynamically pass arguments to a function? grimm1111 2 2,168 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Default values of arguments with the same id DrVictor 3 2,269 Dec-02-2020, 03:27 PM
Last Post: deanhystad
  Function parameters and values as string infobound 1 1,756 Jul-24-2020, 04:28 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020