And, yeah, if there's one screw-up in the built-in Python classes IMO, it's the
datetime module's naming conventions.
datetime is the module name. That module contains several classes, among them
datetime. So, when you:
- Code: Select all
import datetime
you're importing the entire
datetime module. If you want to reference any of the classes/functions defined in it, then you have to use the dot notation:
- Code: Select all
datetime.datetime # a datetime class
If you're only interested in the
datetime class itself, just import the class rather than the entire module:
- Code: Select all
from datetime import datetime
This is my preference - because
datetime.datetime is a lot to type (and it looks really stupid LOL). Other modules name classes with a leading uppercase character (or PascalCase) which helps make it clearer (and, as it turns out, it's in the
PEP8 style guide). For instance, the
collections module has the awesome
OrderedDict class (which is a standard Python dictionary except that when you iterate you get the key/values in the order they were added to the dict - so it iterates more like a list). So, it's clear when you look at a usage of
OrderedDict that it's a class.
I'd like to see:
datetime.DateTime,
datetime.Date,
datetime.Time,
datetime.TimeDelta, etc. But, of course, such a fundamental change isn't backwards compatible so it would cause lots of breakage.