Return a singleton from tzutc()#497
Conversation
| UTC0 = tz.tzutc() | ||
| UTC1 = tz.tzutc() | ||
|
|
||
| self.assertIsNot(UTC0, UTC1) |
There was a problem hiding this comment.
I don't know why I was testing this.
|
To what extent does this complement vs duplicate pytz.utc? |
|
@jbrockmendel |
|
@abalkin Any thoughts on this? Not sure who else would understand the implications of this well enough to review. |
|
This looks like a no-brainer optimization. There are several code paths in the datetime module where having the same tzinfo rather then just equal TZ objects saves several calls to utcoffset() methods. |
|
@abalkin Thanks. That's pretty much what I thought. This is the least controversial of the singletons anyway. I'm going to try and come up with an interface for the others that's not totally nuts but ports these "fast path" qualities to the other types (particularly |
4f25140 to
a4f7cb6
Compare
Python assumes that
tzinfoobjects representing the same zone are effectively singletons, which is why intra- vs inter-zone semantics apply based ondt1.tzinfo is dt2.tzinfo, notdt1.tzinfo == dt2.tzinfo.tzutc()is the simplest case, since it only ever represents one zone. I've also added a convenience constant to the top leveltz/__init__.py, so people can just use it as a constant.Eventually the other classes will store their representations in caches (though this is more complicated and has more implications for the API long term).