[Tutor] pythonic
Alan Gauld
alan.gauld at btinternet.com
Mon Apr 2 04:56:16 EDT 2018
On 02/04/18 04:19, Steven D'Aprano wrote:
> 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.
Aha! That makes sense. I've never really used format() so have never
bothered to find out how it works. To the point that until this thread I
hadn't realized we even had a __format__() operator.
As I said, I need to do some reading. Obviously a gap in my python
education.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list