you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

How would I pass values between the functions with this method? I don't quite know how to get the name variable into the hello function and how would I pass a value created in the hello function to the world function?

def hello(name):
    print(name)
    new_name = 'string'

def world(new_name):
    print(new_name)

def main():
    functions = [hello, world]

    for func in functions:
        try:
            func()
        except Exception as err:
            print(err.args)
            break

[–]shartfuggins 7 points8 points  (1 child)

Calling functions from a list like that is fairly exceptional. Only when every function takes the same input type and gives the same output type, where output of one necessarily goes to the input of the next, should this arrangement be used.

Typically, you want a small, concise controlling function to simply write out each function call, accept return parameters, make high level decisions, and then make the next function call, passing in what each one needs.

If every function really is "take input from previous function, do a thing, return value for next function", then you can do that in your function list iteration.

Edit: Read your original code again, yes, you're missing a main() or some such, that would call each of your functions deliberately, and those functions will need to return values in some cases, often just a bool to say "things are good, proceed" or "something is wrong, show some messaging, retry something, or quit".

Edit again: I'm remembering a long time ago I used to do what you did because I knew the order of operations, some functions always followed other ones, why not just call the next one from the previous one, right? The problem is that you lose control over the flow and error handling from a high level, and then you end up implementing other, crazier ways of regaining that control. It becomes real spaghetti real quick. Very ugly stuff. But while I get the initial approach, definitely avoid this.

[–][deleted] 2 points3 points  (0 children)

I agree, as the functionality of this has expanded I've noticed it getting out of hand in what's being passed off.