more pythonic way

Grant Edwards grant.b.edwards at gmail.com
Sat Feb 16 08:33:37 EST 2019


On 2019-02-16, Barry <barry at barrys-emacs.org> wrote:
> On 11 Feb 2019, at 20:00, Felix Lazaro Carbonell <felix at epepm.cupet.cu> wrote:
>
>>> The most pythonic way is to do this:
>>> 
>>> def find_monthly_expenses(month=datetime.date.today().month,
>> year=datetime.date.today().year):
>>>    ...
>
> This has subtle bugs. 
> The default is calculated at import time and not at function call time.

Indeed.  That was the entire point of that posting: it was an example
of a not-uncommon misunderstanding about when intializer values are
determined.  Unfortunately the subsequent remark about initializers
not working that way has been lost in the shuffle.

> Also the defaults for month and year are not calculated at the same
> moment in time.

Yep.  The race condition between the evaluation of the two initializer
expressions is a bit more subtle than the fact that the evaluations
only happen once at compile time.

-- 
Grant




More information about the Python-list mailing list