Why date do not construct from date?

Chris Rebert clp2 at rebertia.com
Tue Jun 2 08:14:22 CEST 2009


On Mon, Jun 1, 2009 at 10:25 PM, Alexandr N Zamaraev <tonal at promsoft.ru> wrote:
> Simple example:
> [code]
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]
> on win32
>>>> import datetime as dt
>>>> dt.date(2009, 10, 15)
> datetime.date(2009, 10, 15)
>>>> d = dt.date(2009, 10, 15)
>>>> dt.date(d)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: function takes exactly 3 arguments (1 given)
>>>>
> [/code]
> Why int form int, str from str, Decumal from Decumal can construct bat date
> from date not?

Probably because the function signatures would be so different. str(),
int(), etc *always* take *exactly one* argument -- the object to
convert. In contrast, date() takes several integers corresponding to
the year, month, and day. Adding a second signature to it that took
exactly one argument (of type `date`) and copied it would be
significantly different from its other signature; in idiomatic Python,
one would typically make a separate, new function for this drastically
different signature.
However, the `date` type is immutable, so there's no reason at all to
try and copy a new instance from an existing one anyway, thus a
single-argument copy-constructor is completely unnecessary, hence why
there isn't one.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list