![]() |
Function with many arguments, with some default values - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Function with many arguments, with some default values (/thread-24920.html) |
Function with many arguments, with some default values - medatib531 - Mar-10-2020 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? RE: Function with many arguments, with some default values - ndc85430 - Mar-11-2020 Why do you have a function that needs so many things? That's a sign that it's probably doing too much. RE: Function with many arguments, with some default values - deanhystad - Mar-11-2020 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. 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.
RE: Function with many arguments, with some default values - medatib531 - Mar-14-2020 Think this works for me, thanks! |