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) Where on Earth do Timezones Make Sense? Datetime Awareness

Justin Noor
Justin Noor
3,692 Points

Why are some python libraries written in C?

I've never understood why many Python libraries, such as the datetime library, are written in C. I thought Python is a language on its own. Can anyone shed some light on this?

For example if you go to the Python docs and look at the datetime library, and click on the source code at the top left, it will take you to the Mercurial/CPython library page. What is CPython?

Peter Lawless
Peter Lawless
24,404 Points

I've heard that Python is built on top of C.

2 Answers

Jacob Herrington
Jacob Herrington
15,835 Points

The most popular implementation of Python is CPython which is written in C - meaning that the interpreter is written in C. Therefore, if you are using CPython it is possible to add features to your Python code with C. In a manner of speaking, by writing C code you are able to extend CPython itself which allows you to do things like add a new kind of Object.

Here are the source files for CPython on GitHub. If you'd like to see what I mean.

If you are interested in other implementations of Python including a Java implementation and a mind boggling version of the Python interpreter written in Python itself, here is some documentation to introduce you to the concept.

Furthermore, if you'd like to use C with Python yourself, this might interest you.