Bizarre behavior with mutable default arguments

Istvan Albert istvan.albert at gmail.com
Sun Dec 30 08:45:46 EST 2007


On Dec 30, 5:23 am, thebjorn <BjornSteinarFjeldPetter... at gmail.com>
wrote:

>    def age(dob, today=datetime.date.today()):
>        ...
>
> None of my unit tests caught that one :-)

interesting example I can see how it caused some trouble. A quick fix
would be to write it:

def age(dob, today=datetime.date.today ):

and inside the definition invoke it as today() rather than just today.
That way it still keeps the original spirit of the definition.

i.



More information about the Python-list mailing list