3 ms·
When parameter names are meaningless for the final result def abs_days_difference(date1, date2, /): # note that swapping passed params will yield the sam
by alkh 2y ago
When parameter names are meaningless for the final result
def abs_days_difference(date1, date2, /):
# note that swapping passed params will yield the same result
delta = abs((date2 - date1).days)
return delta
d1 = date(2023, 1, 1)
d2 = date(2024, 1, 15)
abs_days_difference(d1,d2)==abs_days_difference(d2,d1)
The function returns the absolute diff in the number of days, so date1, date2 are meaningless variable names. It looks like then introduce an order(could #1 be the earlier date, while #2 be the later one?). If you don't get users any options it is clear that the order is meaningless
Compare with
def day_difference(earlier_date, later_date):
delta = (later_date - earlier_date).days
return delta
Note that now
d1 = date(2023, 1, 1)
d2 = date(2024, 1, 15)
day_difference(d1,d2)!=day_difference(d2,d1)
If you function signature is day_difference(date1, date2,*) you have to specify params as kwargs only, removing the confusion:
d1 = date(2023, 1, 1)
d2 = date(2024, 1, 15)
#this now doesn't work
day_difference(d1,d2)
# this works and is much clearer for caller
day_difference(earlier_date=d1,later_date=d2)