In my app, I want all my datetime.__str__() to return differently to the default. Is it ok to simply inherit and overwrite the method?
class datetime(datetime):
def __str__(self):
return self.strftime('%d-%m-%y %H:%M:%S')
Any advice would be great.
Generally, you will want to name your new class something other than one defined in the builtin modules, but yes, that is how you do it. Please for the sake of your sanity do not create a class definition using the same name as a predefined class.
I just tried to do the
class datetime(datetime)bit, and it does work, at least in the interpreter, but any python expert will probably laugh or shudder.