Why can't I subclass off of "date" ?

Georg Brandl g.brandl-nospam at gmx.net
Thu Aug 24 14:51:53 EDT 2006


davidfinance at gmail.com wrote:
> Ok, maybe this is a stupid question, but why can't I make a subclass of
> datetime.date and override the __init__ method?
> 
> ---
> 
> from datetime import date
> 
> class A(date):
>     def __init__(self, a, b, c, d):
>         print a, b, c, d
>         date.__init__(self, 2006, 12, 11)
> 
> d = A(1, 2, 3, 4)
> 
> ---
> 
> $ python break_date.py
> Traceback (most recent call last):
>   File "break_date.py", line 9, in ?
>     d = A(1, 2, 3, 4)
> TypeError: function takes exactly 3 arguments (4 given)
> 
> 
> If I make A a subclass of some toy class that is constructed with three
> arguments, it works fine.  Why can't I make "date" the subclass?

You'll have to also override the __new__ method.

Georg



More information about the Python-list mailing list