Best Way to Pass Arguments from One Function to Another in Python

Josh

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

jsumskas
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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass arguments from one function to another function in bash scripting?

From Dev

What is the best way to pass data from one View to another in Xamarin?

From Dev

Best way to pass variables from one Swift class to another?

From Dev

Best way to pass action button input from one module to another

From Dev

Pass Arguments from One Function to Another Without Calling It

From Dev

Is there a way to pass in all optional arguments to another function?

From Dev

Best way to pass pointer to C function from Python using CFFI

From Dev

Is there a way to pass a random variable defined in one function to another in Python?

From Dev

VBA - Pass arguments from one function to another function without returning to first function

From Python

Is there a way to reference one function parameter from another function parameter in python?

From Dev

Best (and secure) way to call javascript function (with arguments) using a url from another page

From Java

Best way to copy from one array to another

From Dev

python - Pass/Sending return variables from one function to another

From Dev

Pass a list from one function to another in a class in Python

From Dev

How to pass to a function that takes two arguments an element from one list together with each element of another list?

From Dev

What is best way to pass JSON data one Fragment to another Fragment

From Dev

Python 3: How to call function from another file and pass arguments to that function ?

From Dev

Pass parameter from one Powershell function to another

From Dev

Get value from One function and pass it to another

From Dev

Pass value from one function to another to calculate with it

From Dev

How to pass a value from one function to another

From Java

Best way to check function arguments?

From Dev

JavaScript: Pass unknown number of arguments from one method to another

From Dev

How can I pass arguments from one tab to another?

From Dev

What is the best way to send a list of arguments to a function within a class in Python?

From Dev

In Python can you pass two arguments to a function where one argument is extrapolated from the other?

From Dev

Whats the best way to call one async function inside another?

From Dev

Pass Python function from outside to another function

From Dev

Best practice MVVM pass Data from one Activity to another

Related Related

  1. 1

    How to pass arguments from one function to another function in bash scripting?

  2. 2

    What is the best way to pass data from one View to another in Xamarin?

  3. 3

    Best way to pass variables from one Swift class to another?

  4. 4

    Best way to pass action button input from one module to another

  5. 5

    Pass Arguments from One Function to Another Without Calling It

  6. 6

    Is there a way to pass in all optional arguments to another function?

  7. 7

    Best way to pass pointer to C function from Python using CFFI

  8. 8

    Is there a way to pass a random variable defined in one function to another in Python?

  9. 9

    VBA - Pass arguments from one function to another function without returning to first function

  10. 10

    Is there a way to reference one function parameter from another function parameter in python?

  11. 11

    Best (and secure) way to call javascript function (with arguments) using a url from another page

  12. 12

    Best way to copy from one array to another

  13. 13

    python - Pass/Sending return variables from one function to another

  14. 14

    Pass a list from one function to another in a class in Python

  15. 15

    How to pass to a function that takes two arguments an element from one list together with each element of another list?

  16. 16

    What is best way to pass JSON data one Fragment to another Fragment

  17. 17

    Python 3: How to call function from another file and pass arguments to that function ?

  18. 18

    Pass parameter from one Powershell function to another

  19. 19

    Get value from One function and pass it to another

  20. 20

    Pass value from one function to another to calculate with it

  21. 21

    How to pass a value from one function to another

  22. 22

    Best way to check function arguments?

  23. 23

    JavaScript: Pass unknown number of arguments from one method to another

  24. 24

    How can I pass arguments from one tab to another?

  25. 25

    What is the best way to send a list of arguments to a function within a class in Python?

  26. 26

    In Python can you pass two arguments to a function where one argument is extrapolated from the other?

  27. 27

    Whats the best way to call one async function inside another?

  28. 28

    Pass Python function from outside to another function

  29. 29

    Best practice MVVM pass Data from one Activity to another

HotTag

Archive