How best to test functions which use date.today

rdmurray at bitdance.com rdmurray at bitdance.com
Sat Feb 28 15:34:35 EST 2009


Christian Heimes <lists at cheimes.de> wrote:
> Lie Ryan wrote:
> >> But this fails with:
> >>
> >> TypeError: can't set attributes of built-in/extension type
> >> 'datetime.date'
> > 
> > This is because today is an attribute. In python, we can override
> > attribute access to become a function call. I don't have python right
> > now, but try this:
> > 
> > del date.today
> > date.today = mytoday
> 
> It won't work. The datetime module is written in C. You can't modify a C
>  extension.

Hmm.  Given that, Lie, maybe what you need to do is modify your code
so that you call your own special purpose function to get 'today',
and replace _that_ for testing, using datetime's today for production.

--RDM




More information about the Python-list mailing list