python - Design and structure of batch/group operation functions -
assuming want design function in python takes input one-dimensional (1d) list, processes list , returns processed result.
def process_list(li): # processing. return result li = [elem_1,elem_2,elem_3] process_list(li)
now, there occasions might want process many 1d-lists. in these instances, there many 1d-lists in matrix-like structure (two-dimensional list or, if prefer, list of 1d-lists). each row represented 1d-list.
from design point of view, build on existing function process_list(li)
so:
def process_many_lists(many_li): results = [ process_list(row) row in many_li ] return results many_li = [ \ [elem_11,elem_12,elem_13], \ ..., \ [elem_n1,elem_n2,elem_n3] \ ] process_many_lists(many_li)
alternatively, remove original process_list(li)
function , refactor so:
def process_any_amount_of_lists(li_or_many_li): results = [] row in li_or_many_li: # processing. return results li_or_many_li = [ \ [elem_11,elem_12,elem_13], \ ..., \ [elem_n1,elem_n2,elem_n3] \ ] process_any_amount_of_lists(li_or_many_li)
the first approach allows me keep separate dedicated function instances 1 list processed (these instances occur) through process_list(li)
, but, arguably, it's redundant. in addition, convolutes process_many_lists(many_li)
function because works calling process_list(li)
.
the second approach using process_any_amount_of_lists(li_or_many_li)
removes separate dedicated function instances 1 list processed, but, perhaps eradicates redundancy , convolution previous approach not rely on process_list(li)
. it's worth noting that, using approach, instances 1 list processed, 1 list must still presented function 2d list (e.g. li = [[elem_1,elem_2,elem_3]]
not li = [elem_1,elem_2,elem_3]
). of course, additional code include preliminary step perform different actions depending on dimensionality of list, situation having 2 separate functions anyway.
- is there clear answer here route should taken in scenario this?
- perhaps alternative approach not described above preferred?
- is solution exclusively "pythonic" or more general?
Comments
Post a Comment