Calling a list of functions

BartC bc at freeuk.com
Sun Dec 13 12:38:13 EST 2015


On 13/12/2015 17:26, Ganesh Pal wrote:
> Iam on linux and python 2.7 . I have a bunch of functions which I
> have run sequentially .
> I have put them in a list and Iam calling the functions in the list as
> shown below , this works fine for me , please share your
> opinion/views on the same
>>> Sample code :
>> def print1():
> print "one"
>> def print2():
> print "two"
>> def print3():
> print "three"
>> print_test = [print1(),print2(),print3()] //calling the function
>> for test in range(len(print_test)):
> try:
> print_test[test]
> except AssertionError as exc:
>
 > I have put them in a list and Iam calling the functions in the list as
 > shown below , this works fine for me , please share your
That's not quite what the code does, which is to call the three 
functions and put their results into the list (3 Nones I think).
Then you evaluate each element of the list (a None each time).
I had to modify it to the following, which sets up a list of the three 
functions, then calls them in turn using the loop. I don't know what the 
'except' part was supposed to do:
def print1():
 print "one"
def print2():
 print "two"
def print3():
 print "three"
print_test = [print1,print2,print3]	#NOT calling the function
for test in range(len(print_test)):
 try:
 	print_test[test]() #calling the function
 except AssertionError:
 	pass
The output of the two programs would have been the same I think.
-- 
Bartc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /