On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote: > On01/04/18 20:20, Albert-Jan Roskam wrote: > > fmt="%Y-%m-%d %H:%M\n" > > f.write(now.strftime(fmt)) > > Lately I've been using format(), which uses __format__, because I find it > > slightly more readable: > > format(datetime.now(), "%Y-%m-%d %H:%M") > Interesting, > I didn't know that format() recognised the datetime format codes.
It doesn't. It is the datetime object that recognises them. format() merely passes the format string to the datetime.__format__ method, which is what recognises the codes. It doesn't care what it is. py> class Spam: ... def __format__(self, template): ... return template.replace("^DD^", " surprise ") ... py> format(Spam(), "some^DD^string") 'some surprise string' -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor