[Python-Dev] factory functions in the datetime module (& others)

Brian Quinlan brian at sweetapp.com
Wed May 12 13:42:55 EDT 2004


It seems to me that the utility of subclassing certain library classes would 
be increased if the user could set a factory that the base class would use 
when creating new instances. That way, from the user's point of view, 
functionality would not "disappear" after operations e.g.

class MyDate(datetime.date):
     def foo(self):
         ...

nextday = MyDate(...) + datetime.timedelta(days=1)
nextday.foo() # exception

Storing the factory information in the base class has the disadvantage of 
causing a global behavior change but can probably be made speed efficient. 
Storing the factory in the subclass would require that the super class check 
for a class variable when creating instances. It would probably provide the 
most obvious semantics from the programmer's POV but would be organizationally 
odd i.e. each class would have to provide a factory for every overriden type 
that it's superclass creates.

Aside from datime.* this feature seems like it would be useful for other types 
where there is potential for making useful subclasses (unlike int) and where 
many operations result in new instances. sets come to mind.

Any thoughts?

Cheers,
Brian



More information about the Python-Dev mailing list