if else-if making code look ugly any cleaner solution?
Posted
by Vishal
on Stack Overflow
See other posts from Stack Overflow
or by Vishal
Published on 2010-05-05T19:21:07Z
Indexed on
2010/05/05
19:38 UTC
Read the original article
Hit count: 145
python
I have around 20 functions (is_func1, is_fucn2, is_func3...) returning boolean
I assume there is only one function which returns true and I want that!
I am doing:
if is_func1(param1, param2):
# I pass 1 to following
abc(1) # I pass 1
some_list.append(1)
elif is_func2(param1, param2):
# I pass 2 to following
abc(2) # I pass 1
some_list.append(2)
...
.
.
elif is_func20(param1, param2):
...
Please note: param1 and param2 are different for each, abc and some_list take parameters depending on the function.
The code looks big and there is repetition in calling abc and some_list, I can pull this login in a function! but is there any other cleaner solution?
I can think of putting functions in a data structure and loop to call them.
© Stack Overflow or respective owner