more pythonic way

Peter Otten __peter__ at web.de
Mon Feb 11 15:54:34 EST 2019


Felix Lazaro Carbonell wrote:

> Hello to everyone:

 
> Could you please tell me wich way of writing this method is more pythonic:
>     def find_monthly_expenses(month=None, year=None):
> 
>         month = month or datetime.date.today()

> Or it should better be:

>         if not month:
>             month = datetime.date.today()

Personally I would avoid a default because I'm unsure whether the current or 
the previous month is the right default. 

Also, the default month combined with a specific year doesn't make much 
sense to me, and someone who specifies find_monthly_expenses(month=6) in May 
probably wants the June of the past year...

Keep the function simple an make the arguments non-optional. If you can come 
up with a nice heuristic put it in a separate function.




More information about the Python-list mailing list