0

I have a function that performs a specific task, and this function takes many options, both named and unnamed options. For example:

def eat_fruit(x,a_number=None , a_fruit=None):
    return(f'{x} ate {str(a_number)} {a_fruit}')
#call the function
eat_fruit("John",a_number=5,a_fruit='apples') #outputs 'John ate 5 apples'

Now, I have another function that takes many options, for example:

def do_lots_of_stuff(a,b,activity_1=None,activity_2=None):
    return(f'''{a} and {b} {activity_1} and {activity_2}''')
do_lots_of_stuff("Bob","Mary",activity_1='run',activity_2='jump') #returns "Bob and Mary run and jump"

I want to have the function do_lots_of_stuff call the function eat_fruit, sometimes with options. However, it is not clear to me how to pass options from one to the other in a straightforward manner.

Ideally, I am looking for something like:

#This code does not work; it demos the type of functionality I am looking for.
do_lots_of_stuff("Bob","Mary",activity_1='run',activity_2='jump', eat_fruit_options={*put_options_here*}):
    eat_fruit(eat_fruit_options)
    return(f'''{a} and {b} {activity_1} and {activity_2}''')

Note that this can't be accomplished via do_lots_of_stuff(*do_lots_of_stuff_options*, *eat_fruit_options*) since options for eat_fruit are not valid do_lots_of_stuff options. Furthermore, keyword arguments must come after positional arguments. In addition this solution does not seem to be sufficient here, because I only want to pass some arguments, not all of them.

Other relevant links (although I don't believe they successfully address my question):

can I pass all positional arguments from one function to another in python?

Passing variables between functions in Python

Passing value from one function to another in Python

Josh
  • 1,155
  • 4
  • 12
  • 21

1 Answers1

0
do_lots_of_stuff("Bob","Mary",activity_1='run',activity_2='jump', eat_fruit_args=["John"], eat_fruit_kwargs={"a_number": 5, "a_fruit": "apples"}):
        eat_fruit(*eat_fruit_args, **eat_fruit_kwargs)
        return(f'''{a} and {b} {activity_1} and {activity_2}''')

You can pass and forward arguments and keyword arguments. Arguments are in the form of a list. Keyword arguments (kwargs) are in the form of a dictionary, with the key as a string, and the value as the correlating keyword value.

jsumskas
  • 86
  • 4