0

I am learning Python and struggling to understand on how to send multi-values to the next function after returning the value from the first function? Also is it necessary to use main() in Python to call more than one functions?

In the following code, I would want to pass both acc_name and rg_name to the function stop().

I have this following simplified code, where the logic works as expected, hence have not included that as I just want to understand the workflow of the code.

    def handle(event, context):
       #code logic
       return acc_name, rg_name
    def stop(acc_name, rg_name):
        #code logic
    return sg_id

OR

    def handle(event, context):
       #code logic
       return acc_name, rg_name
    def stop(x,y):
        #code logic
    return sg_id

    def main():
      x,y = handle(event, context)
      stop(x,y)

I am a newbiew to Python, my code might have discrepancies from the concept. Using Python 2.7

Any help would be appreciated. Thanks in advance

user1725651
  • 139
  • 1
  • 3
  • 11
  • You never call `stop` in the first chunk. – Carcigenicate Jan 29 '17 at 22:41
  • what's exactly the problem with the second chunk? It seems to do what you want. – Rémi Bonnet Jan 29 '17 at 22:42
  • Also, it's unclear what you're even asking here. I only see one question here regarding `main`, and it's unclear what you mean "if main isn't used". – Carcigenicate Jan 29 '17 at 22:42
  • @Carcigenicate, I want to know if main() is necessary to use in Python while calling more than one function? If yes, then is the second chunk the right way to code it? – user1725651 Jan 29 '17 at 22:46
  • @user1725651 a `main` is always necessary if you intend it to be a runnable program. The only time you'd not have a main is if you're writing a library that will be used instead of run directly. – Carcigenicate Jan 29 '17 at 22:48
  • Edited the question. Thanks for the comments – user1725651 Jan 29 '17 at 22:48
  • 1
    @user1725651 and besides the wrong indentation of the return in `stop`, the second bit looks fine; although there's barely any code there to judge. – Carcigenicate Jan 29 '17 at 22:49

1 Answers1

1

I'm going to assume that what you are asking is how to pass multiple arguments from functionA to functionB without storing the arguments in between the two function calls like you did in snippet two. So how to avoid:

x, y = functionA()
functionB(x, y)

This you can do by unpacking the arguments here's the docs for the feature. You do this by calling:

functionB(*functionA())

or in your case:

stop(*handle(event, context))
Vincent
  • 536
  • 4
  • 8
  • @user1725651 I'll just add though that while this is a handy shortcut, you may find that your code is more readable in the longer, more explicit form. Brevity != more readable in many cases. Try both and see what reads better. – Carcigenicate Jan 29 '17 at 23:19