I've written a program that performs some complex tasks. I thought the best way to make sure my code is neat and concise was to define multiple functions. Unfortunately, by doing this, I have come across a problem where I am having to return multiple values from various functions for use in other functions and am getting tripped up by it all.
Here is essentially my program but simplified, paste it in IDLE and it should work for you.
def function_zero():
a = ['1', '2', '3']
b = ['4', '5', '6']
c = ['0']
d = ['0']
return a, b, c, d
def function_one(a):
for i in a:
function_two(i)
# m, l = function_two(i)
def function_two(i):
morel = []
lessl = []
sum_i = int(i)*2
if sum_i >= 3:
morel.append(i)
if sum_i <= 3:
lessl.append(i)
# return m, l
def function_three(a, b, c, d):
#def function_three(a, b, c, d, m, l):
print a, b, c, d
#print m, l
def main():
a, b, c, d = function_zero()
function_one(a)
function_three(a, b, c, d)
# function_three(a, b, c, d, e, f)
if __name__ == '__main__':
main()
Function zero parses a report, generating values a, b and so on. Function one actually uses itertools and izip to merge a and b together, but for simplicity this is hidden. Essentially what this does is perform a command which requires two arguments, and it's important that these commands are done in iteration.
Function two introduces the main problem here, list objects l and m. I do not know where these should be initiated, if I initiate them outside the functions, it can become messy, so I've done it inside function two. I also have severe problems trying to print them in function three (Function three is a reporting function) due to NameError's and unresolved references.
Without having to rewrite the functions and making major modifications (this would mean I would have to rewrite my program if someone suggested a simpler way of doing this, and if I adopted it, it may mean that I lose functionality elsewhere). Can anyone tell me how I would sort of pass m and l which are initiated in function two, up to function one for them to be accessible in function three?
Any advice would be greatly appreciated, as you can see from the commented code - I have tried to fix the problem but I thought I would ask for some help before hacking away helplessly.
function_zeroand when you callfunction_onewith an argument of one of those references. Do something similar withfunction_two.itofunction_twopass the whole ofaand returnapartioned intom, l = function_two(a). It is unclear what you plan to do with those partitioned values. BTW there is anitertoolsrecipe calledpartition()that might be useful.