[Tutor] question about object oriented programming and inheritance using datetime module

Kent Johnson kent37 at tds.net
Tue Jan 16 20:53:25 CET 2007


Terry Carroll wrote:
> An example is if you wanted to create a "birthdate" class, which was just 
> like a regular date, but also included the birthstone that corresponded to 
> the date.  We could create a "birthdate" module that included a 
> "Birthdate" class:
> 
> ###############
> 
> import datetime
> 
> class Birthdate(datetime.date):
> 
>     def __init__(self, year, month, day):
>         stones = ["Garnet", "Amethyst", "Aquamarine",
>           "Diamond", "Emerald", "Perl",
>           "Ruby", "Python", "Sapphire",
>           "Opal", "Topaz", "Turquoise"]
>         self.birthstone = stones[month-1]

I think you are missing the line
   datetime.date.__init__(self, year, month, day)

somewhere in here.

I am very surprised that this works, my understanding was that 
datetime.date was immutable and required overriding __new__() rather 
than __init__(). But it does work...

Kent



More information about the Tutor mailing list