-
Notifications
You must be signed in to change notification settings - Fork 523
Return a singleton from tzutc() #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| UTC0 = tz.tzutc() | ||
| UTC1 = tz.tzutc() | ||
|
|
||
| self.assertIsNot(UTC0, UTC1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).