[Tutor] pythonic

Mats Wichmann mats at wichmann.us
Mon Apr 2 08:49:52 EDT 2018


On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote:
> 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.
> 

so since we're all learning things here, how would this play out with
the new f-strings?


More information about the Tutor mailing list