sub-classing datetime

marduk marduk at python.net
Thu Feb 7 14:16:14 EST 2013



On Thu, Feb 7, 2013, at 10:13 AM, Colin J. Williams wrote:
> I'm just making the transition from 2 to 3 for one module.
> 
> With Python 2.7, I had the benefit of mx datetime, but this is not yet 
> available for Python 3.2.
> 
> I find that the 3.2 datetime is not subclassable, for reasons that were 
> known some years back.
> 
> It would help if there was a note in the docs listing the builtin 
> classes which are not subclassable.
> 
> I am retreating to the use of a function.

What makes you think it's not subclassable?:

import datetime

class MyDateTime(datetime.datetime):
    def what_date(self):
        print(self)


md = MyDateTime.now()
md.what_date()

Seems to work even in 2.7 (excluding the print function).




More information about the Python-list mailing list