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
  Checking the number of arguments a function takes Chirumer 3 201 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Function - Return multiple values tester_V 10 806 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Possible to dynamically pass arguments to a function? grimm1111 2 483 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Default values of arguments with the same id DrVictor 3 542 Dec-02-2020, 03:27 PM
Last Post: deanhystad
  Function parameters and values as string infobound 1 558 Jul-24-2020, 04:28 AM
Last Post: scidam
  How to pass multiple arguments into function Mekala 4 881 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 701 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  How to give a name to function arguments in C-API? WonszZeczny 0 490 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  Return values for use outside of function willowman 1 637 Apr-13-2020, 07:00 AM
Last Post: buran
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 874 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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