Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Where on Earth do Timezones Make Sense? Actually, Use pytz Instead

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Actually, Use pytz Instead

So here is something I don't understand about Python.

In this video, we see a few useful variables set such as: pacific = pytz.timezone('US/Pacific') utc = pytc.utc

These seem to make so much sense, that I wonder why they aren't simply defined, or set, by default in the pytx library.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Because they won't always be needed by everyone and Python is explicit. You shouldn't do anything your users haven't asked you to do (you being a library author and your users being developers using your library).

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

So everything in a library does something? Nothing is ever set or predefined? As I go through learning Python, it seems like some things have been predefined. Maybe to avoid confusion, the developers decided to keep the number of predefined things to a minimum?

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

It does seems like Python has predefined some things because re.M and re.MULITLINE do the same thing (re.M = re.MULTILINE, correct?), so it is hard to understand (from a beginners point of view) why they don't set other things like utc = pytz.utc. But again, I could see doing so as potentially causing confusion if a user did not know which variables were pre-set, and then tried to create a variable of their own that they chose to call utc.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Of course there will be some presets. Not all developers agree on what should and shouldn't be explicit/implicit. But something like having a predefined attribute for every timezone always attached to the library is a bit overkill.

As for pytz.utc, it is already set. It just happens to be namespaced (inside the pytz library). You wouldn't want them to automatically set a variable in your namespace. What if you already had a variable named utc and they overwrote it?

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Yes, thanks, that's essentially what I pointed out in my last sentence. It almost feels like it would be useful to be able to pull in a file with all of the time zones variables preset. Maybe people do that.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, sorry, saw that right as I was submitting the comment.

It could be handy, yes, but that would probably be a pretty special case (when you'd need all of them at once).