Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times Manipulating Time Already

Aren't classes always capitalized, e.g. Math? In the expression datetime.datetime which is the class?

Seems to me that when working with datetime values we should get that functionality by typing

from datetime import Datetime

just as we did when creating the monster game:

from monster import Monster

I assume that the first datatime in the expression datetime.datetime is the namespace or module and the second instance of datetime is Python's datetime class. If that's true, shouldn't it really be written as datetime.Datetime?

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Objects and classes are, for most purposes, exactly the same thing. I guess you could think of an object as being an instance of a class, though, which makes them sorta different. Anyway...

datetime is the library (like your monster.py file). datetime.datetime is the class. So why isn't it capitalized? Well, why isn't list or str or dict capitalized? Python's internal tradition seems to be that older and very commonly used classes aren't capitalized. I'm sure there's a historical reason, but I don't personally know it.

I hear you. It's like C# or Java, which capitalize String objects but not int, float etc, even though they're all objects, technically speaking. Thank you.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, it's weird and inconsistent (sadly). Typically it's the older things. You just end up memorizing which is which or rely on documentation.

Gavin Ralston
Gavin Ralston
28,770 Points

datetime is a builtin module, and datetime.datetime is a builtin class, so capitalization isn't used. If it's in the batteries that are included with python by default, I think that's the rule.

PEP 8 on Naming Conventions

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Adiv.

"from datetime import Datetime"

We haven't used this ( I think) as we need to import the whole datetime library, with all its peculiarities, so we just import datetime.

Regarding the datetime.datetime I think that is just the was to call that object and not a class.

Maybe someone will have a more exhaustive answer to the second part of your question.

Vittorio