In my current project I have some duplicated code and I am looking for a possibility to remove it. I have a boolean called forward. If it's true, I want to add days to a datetime, if not I want to subtract days:
if forward:
day = today + datetime.timedelta(days=3)
else:
day = today - datetime.timedelta(days=3)
Is there any possibility to do that in less than these 4 lines?
(operator.add if forward else operator.sub)(today, datetime.timedelta(days=3))… But is that really more readable…?!