Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38360

Re: sub-classing datetime

From marduk <marduk@python.net>
References <kf0gb1$dal$1@theodyn.ncf.ca>
Subject Re: sub-classing datetime
Date 2013-02-07 14:16 -0500
Newsgroups comp.lang.python
Message-ID <mailman.1456.1360264582.2939.python-list@python.org> (permalink)

Show all headers | View raw



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).

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

sub-classing datetime "Colin J. Williams" <cjw@ncf.ca> - 2013-02-07 10:13 -0500
  Re: sub-classing datetime marduk <marduk@python.net> - 2013-02-07 14:16 -0500

csiph-web