Which implementation of current_quarter
function would be more pythonic, and why?
First version seems to be easier to extend. However, it's a kind of function you would not extend because the notion of a quarter wouldn't change.
In the second version it is easier to grasp the mechanics.
def current_quarter(input_date):
months_baskets = [(1,2,3), (4,5,6), (7,8,9), (10,11,12)]
quarters = (1,2,3,4)
for idx, basket in enumerate(months_baskets):
if input_date.month in basket:
return quarters[idx]
break
def current_quarter(input_date):
if input_date.month in (1, 2, 3):
return 1
elif input_date.month in (4, 5, 6):
return 2
elif input_date.month in (7, 8, 9):
return 3
elif input_date.month in (10, 11, 12):
return 4