C
C
copyerfiled2018-02-10 15:22:19
Python
copyerfiled, 2018-02-10 15:22:19

Is it possible to iterate over functions in a loop?

def blablabla():
    if abc1() >2:
        return True
    if zxc2() >2:
        return True    
    ... и тд.

There are 100500 functions, is it possible to implement the same in a loop?
Here is an example:
def abc1():
    print('один')
    return 1

def zxc2():
    print('два')
    return 2

def ffn3():
    print('три')
    return 3

superlist = [
    abc1(),
    zxc2(),
    ffn3(),
    ]

for i in superlist:

    if i > 2:
        print(i, 'нашли')
        break
    print(i, 'перебираем')
else:
        print('Ничего не найдено')

one
two
three
1 iterate
2 iterate
3 found
All functions worked at once at the time of list initialization, but it is necessary that they be called one by one in a loop with a condition. Is there such magic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Romanov, 2018-02-10
@copyerfiled

superlist = [
    abc1,
    zxc2,
    ffn3,
    ]
for function in superlist:
  if function() > 2:
    и так далее

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question