Bizarre behavior with mutable default arguments

thebjorn BjornSteinarFjeldPettersen at gmail.com
Sun Dec 30 05:23:40 EST 2007


On Dec 29, 7:17 pm, Istvan Albert <istvan.alb... at gmail.com> wrote:
> On Dec 29, 12:50 pm, bukzor <workithar... at gmail.com> wrote:
>
> > Is this functionality intended? It seems very unintuitive. This has
> > caused a bug in my programs twice so far, and both times I was
> > completely mystified until I realized that the default value was
> > changing.
>
> it is only unintuitive when you do not know about it
>
> once you realize how it works and what it does it can actually be very
> useful
>
> i.

I agree it is a potentially useful feature, yet it can still bite you
even after a decade of Python... Scenario: long running server
process, Bug report: "people aren't getting older", Code:

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

None of my unit tests caught that one :-)

-- bjorn



More information about the Python-list mailing list