I understand that repr()‘s purpose is to return a string, that can be used to be evaluated as a python command and return the same object. Unfortunately, pytz does not seem to be very friendly with this function, although it should be quite easy, since pytz instances are created with a single call:
import datetime, pytz
now = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
repr(now)
returns:
datetime.datetime(2010, 10, 1, 13, 2, 17, 659333, tzinfo=<DstTzInfo 'Europe/Berlin' CEST+2:00:00 DST>)
which cannot be simply copied to another ipython windows and evaluated, because it returns a Syntax Error on the tzinfo attribute.
Is there any simple way to let it print:
datetime.datetime(2010, 10, 1, 13, 2, 17, 659333, tzinfo=pytz.timezone('Europe/Berlin'))
when the 'Europe/Berlin' string is already clearly visible in the original output of repr()?
Note that
pytz.timezone("Europe/Berlin")in the summer can mean something different thanpytz.timezone("Europe/Berlin"))in the winter, due to daylight savings time. So the monkeypatched__repr__is not a correct representation ofselffor all time. But it should work (except for extreme corner cases) during the time it takes to copy and paste into IPython.An alternative approach would be to subclass
datetime.tzinfo: