Still the __new__ hell ...

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Mar 19 08:17:11 EDT 2007


Paulo da Silva a écrit :
(snip)

Not an answer to your question, just a couple advices:

> from datetime import date
> import cPickle,string

The string module is mostly deprecated. You should use str type methods 
whenever possible (cf below)

> class MyDate(date):
> 	def __new__(cls,year,month=None,day=None):
> 		if type(year) is str:

And what if it's a unicode string ?
The correct idiom here is:
                  if isinstance(year, basestring):

> 			year,month,day=map(int,string.split(year,'-'))
                         year, month, day = map(int, year.split('-'))

> 		if year < 100:
> 			year += 2000
> 		return date.__new__(cls,year,month,day)
> 
(snip)



More information about the Python-list mailing list