Better behavior of DstTzInfo.__repr__ method

Asked by Hyun-goo Kang

I suggest DstTzInfo instance should display its UTC offset based on current datetime. We could reduce some confusion on inspecting timezone instances by changing behavior of DstTzInfo.__repr__ method. This would make the code more explorable. Many newcomers run REPL and check how the example code work. I guess we should consider them as the majority of use case.

I am expecting your various opinions.

Thank you.

Question information

English Edit question
pytz Edit question
No assignee Edit question
Solved by:
Hyun-goo Kang
Last query:
Last reply:
Revision history for this message
Stuart Bishop (stub) said :

What is happening is that there is not a single DstTzInfo for a time zone, but several; one for each distinct period. The __repr__ method is what we have to tell them apart.

>>> from pytz import timezone
>>> from datetime import datetime
>>> timezone('Europe/Paris')
<DstTzInfo 'Europe/Paris' PMT+0:09:00 STD>
>>> timezone('Europe/Paris').localize(
<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>

For newcomers, an alternative is for the DstTzInfo returned from pytz.timezone(...) to be the DstTzInfo for the current time. At the moment, it returns the DstTzInfo for the first period in history. This is almost always not what you want, and forces correct usage to get correct answers. I worry that returning the DstTzInfo for the current time will mean people don't notice their usage error and subtle bugs will end up in their code.

Revision history for this message
Hyun-goo Kang (cyluss) said :

I got the points.