Calling a list of functions

Grant Edwards invalid at invalid.invalid
Sun Dec 13 12:41:27 EST 2015


On 2015-12-13, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> Hi Team,
>
> 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 no clue what your actual goal is, but it might be better to do
the function call in the try/except block inside the loop. Otherwise
your try/except block makes no sense because there's nothing being
executed inside it:

for test in [print1,print2,print3]:
    try:
        test()
    except AssertionError as exc:
        print exc

        







More information about the Python-list mailing list