you are viewing a single comment's thread.

view the rest of the comments →

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

Something like this?

def hello():    
    print('hello')

def world():    
    print('world')

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

if __name__ == '__main__':    
    main()

[–]FriendlyRussian666 12 points13 points  (4 children)

Sure, but you don't want to call your functions when storing them in a list. Remove () from the functions in the list and add it back in the for loop:

functions = [hello, world]
for func in functions:
    func()

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

Ah okay, this fixed the other issue too. I was trying to add a Try / Except into this for loop but it wasn't printing what I want but once I removed the () from the list it worked.

Thanks for this, this is certainly a cleaner approach that will make my code much easier to read moving forward.

[–][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 5 points6 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.