Why date do not construct from date?

Peter Otten __peter__ at web.de
Tue Jun 2 03:45:19 EDT 2009

Gabriel Genellina wrote:

> If one can say float(3.0), str("hello"), etc -- what's so wrong with
> date(another_date)?

You can do

x = float(x)

when you don't know whether x is a float, int, or str. Not terribly useful, 
but sometimes convenient because making the float() call idempotent allows 
you to skip the type check.

def subtract(a, b):
    if isinstance(a, str): a = float(b)
    if isinstance(b, str): b = float(b)
    return a - b


def subtract(a, b):
    return float(a) - float(b)

For date you'd have to make the type check anyway, e. g.

if isinstance(x, tuple): 
   x = date(*x)
   x = date(x) # useless will only succeed if x already is a date

as there would be no other way to create a date from a single value.

So the date() call in the else branch is completely redundant unless you 
change date() to accept multiple types via the same signature:

for x in "2000-01-01", datetime.now(), (2000, 1, 1):
    print date(x)


More information about the Python-list mailing list