[docs] http://docs.python.org/library/datetime.html has a sample which contradicts advice on the page

Justin Grant jgrant at splunk.com
Mon Nov 8 23:36:00 CET 2010


The datetime docs (http://docs.python.org/library/datetime.html) have a code sample which doesn't follow advice from another part of the same page.

First, here's the advice:
Special requirement for pickling: A tzinfo<http://docs.python.org/library/datetime.html#datetime.tzinfo> subclass must have an __init__()<http://docs.python.org/reference/datamodel.html#object.__init__> method that can be called with no arguments, else it can be pickled but possibly not unpickled again. This is a technical requirement that may be relaxed in the future.
Here's the sample:

class FixedOffset(tzinfo):
    """Fixed offset in minutes east from UTC."""

    def __init__(self, offset, name):
        self.__offset = timedelta(minutes = offset)
        self.__name = name

    def utcoffset(self, dt):
        return self.__offset


This sample, if It's following the page's own advice, should have an __init__ method to support pickling which has no required parameters (except self, of course). Something like this:

    def __init__(self, offset_hours=0, offset_minutes=0, name=''):

Of course, this may not be the right behavior either (e.g. might want to create a default name for the time zone based on hours & minutes) but at least by specifying default parameter values you avoid hard-to-troubleshoot exceptions at runtime like your existing sample did for me. :-)

Thanks!

Justin Grant
jgrant at splunk.com<mailto:jgrant at splunk.com>  |  +1 415 848 8578
Splunk  | Product Management

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20101108/5c239a15/attachment-0001.html>


More information about the docs mailing list