Seems that some are written in C and others are written in python? Correct. How does that work? The Python interpreter itself is written in C, so all entities within a Python program are ultimately represented internally by C-level data structures, anyway. You can therefore quite naturally write C-extensions for Python. How did they decide which is which. Performance reasons, primarily. What's implemented directly in C is going to be a lot faster, so anything for which that speed-up matters is going to be. And also, what if you want to extend a built in python class that is written in C? It works much the same as with any other class. While there's some differences between built-in and custom classes, they aren't fundamental. How is a class even created in C which is not an OOP language? You don't need an OOP language to implement an OOP language. In fact, all OOP languages must be implemented by a non-OOP language somewhere along the chain. All code is eventually translated to machine code, as that's the only language the CPU speaks. Machine code does not even really have a notion of variables or functions, much less higher-level concepts like classes. Those features do not make a language inherently more expressive than lower-level ones. It takes very little to create a language that can compute anything that can be computed. Everything else is just there to make the life of programmers easier. In the case of C, it does have structs, which are kind of proto-classes. However, Python classes are actually represented as instances of C structs. You need to remember that Python classes on the C level don't fulfill the function of classes, that's where they are given the function of classes. Edit: But I can't find others like for example the sys module, which I believe is written in C. Where can I find that? You can find the C source code on the CPython GitHub: https://github.com/python/cpython The sys module is here, specifically: https://github.com/python/cpython/blob/main/Python/sysmodule.c Answer from Spataner on reddit.com
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ extending โ€บ extending.html
1. Extending Python with C or C++ โ€” Python 3.14.4 documentation
The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python standard library modules, some written in c some python?
r/learnpython on Reddit: Python standard library modules, some written in C some python?
September 22, 2021 -

Hi. I'm trying to understand the python standard library modules. Seems that some are written in C and others are written in python? How does that work? How did they decide which is which. And also, what if you want to extend a built in python class that is written in C? How is a class even created in C which is not an OOP language? I know that's a lot of questions, but thanks if you can help me understand.

Edit: Also, I can find certain modules like os by looking in the directory /usr/lib/python3.9 (I'm on linux). The os module is in that directory named os.py. But I can't find others like for example the sys module, which I believe is written in C. Where can I find that?

Top answer
1 of 2
7
Seems that some are written in C and others are written in python? Correct. How does that work? The Python interpreter itself is written in C, so all entities within a Python program are ultimately represented internally by C-level data structures, anyway. You can therefore quite naturally write C-extensions for Python. How did they decide which is which. Performance reasons, primarily. What's implemented directly in C is going to be a lot faster, so anything for which that speed-up matters is going to be. And also, what if you want to extend a built in python class that is written in C? It works much the same as with any other class. While there's some differences between built-in and custom classes, they aren't fundamental. How is a class even created in C which is not an OOP language? You don't need an OOP language to implement an OOP language. In fact, all OOP languages must be implemented by a non-OOP language somewhere along the chain. All code is eventually translated to machine code, as that's the only language the CPU speaks. Machine code does not even really have a notion of variables or functions, much less higher-level concepts like classes. Those features do not make a language inherently more expressive than lower-level ones. It takes very little to create a language that can compute anything that can be computed. Everything else is just there to make the life of programmers easier. In the case of C, it does have structs, which are kind of proto-classes. However, Python classes are actually represented as instances of C structs. You need to remember that Python classes on the C level don't fulfill the function of classes, that's where they are given the function of classes. Edit: But I can't find others like for example the sys module, which I believe is written in C. Where can I find that? You can find the C source code on the CPython GitHub: https://github.com/python/cpython The sys module is here, specifically: https://github.com/python/cpython/blob/main/Python/sysmodule.c
2 of 2
1
How is a class even created in C which is not an OOP language? How does a computer understand classes when all it can execute are hex instructions? A class is a concept that can be implemented in any language, some with more ease than others. With C you can mimic a class behavior by having a data structure that gets passed as first argument to all the functions that "belong" to the class.
๐ŸŒ
Python Tips
book.pythontips.com โ€บ en โ€บ latest โ€บ python_c_extension.html
22. Python C extensions โ€” Python Tips 0.1 documentation
In this example the C file is self explanatory - it contains two functions, one to add two integers and another to add two floats. In the python file, first the ctypes module is imported. Then the CDLL function of the ctypes module is used to load the shared lib file we created.
๐ŸŒ
Cython
cython.readthedocs.io โ€บ en โ€บ latest โ€บ src โ€บ tutorial โ€บ clibraries.html
Using C libraries โ€” Cython 3.3.0a0 documentation
As Cython code compiles down to C code itself, it is actually trivial to call C functions directly in the code. The following gives a complete example for using (and wrapping) an external C library in Cython code, including appropriate error handling and considerations about designing a suitable API for Python and Cython code.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ extending โ€บ building.html
4. Building C and C++ Extensions โ€” Python 3.14.4 documentation
A C extension for CPython is a shared library (for example, a .so file on Linux, .pyd on Windows), which exports an initialization function.
๐ŸŒ
Real Python
realpython.com โ€บ build-python-c-extension-module
Building a Python C Extension Module โ€“ Real Python
June 27, 2023 - To write Python modules in C, youโ€™ll need to use the Python API, which defines the various functions, macros, and variables that allow the Python interpreter to call your C code.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ index.html
The Python Standard Library โ€” Python 3.14.4 documentation
The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ ctypes.html
ctypes โ€” A foreign function library for Python
Source code: Lib/ctypes ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these ...
Find elsewhere
๐ŸŒ
Readthedocs
reptate.readthedocs.io โ€บ developers โ€บ python_c_interface.html
Tutorial: Interfacing Python and C code โ€” RepTate 1.3.15 documentation
We use the basic_function.so library via Ctypes. In a new file, write the following and save it as, for example, basic_function_helper.py ยท 1""" 2Define the C-variables and functions from the C-files that are needed in Python 3""" 4from ctypes import c_double, c_int, CDLL 5import sys 6 7lib_path = 'theories/basic_function_%s.so' % (sys.platform) 8try: 9 basic_function_lib = CDLL(lib_path) 10except: 11 print('OS %s not recognized' % (sys.platform)) 12 13python_c_square = basic_function_lib.c_square 14python_c_square.restype = None
๐ŸŒ
NAPALM
saidvandeklundert.net โ€บ learn โ€บ 2022-06-02-extending-python-with-c
Extending Python with C
June 2, 2022 - A good deal of Python, or at least CPython, is written in C. The language and the interpreter are implemented in C and in addition to this, you can also find a lot of modules in the Python standard library powered by C.
๐ŸŒ
Opensource.com
opensource.com โ€บ article โ€บ 22 โ€บ 11 โ€บ extend-c-python
Write a C++ extension module for Python | Opensource.com
November 24, 2022 - In a previous article, I gave an overview of six Python interpreters. On most systems, the CPython interpreter is the default, and also the poll in my last article showed that CPython is the most popular one. Specific to CPython is the ability to write Python modules in C using CPythons extensions API.
๐ŸŒ
ActiveState
activestate.com โ€บ home โ€บ blog โ€บ how to build and install c libraries in python
How to Build and Install C Libraries in Python
March 10, 2025 - C (and less commonly, Fortran) libraries are a necessary evil in Python since they significantly speed up compute-intensive routines, such as math calculations.
๐ŸŒ
Ipython-books
ipython-books.github.io โ€บ 54-wrapping-a-c-library-in-python-with-ctypes
IPython Cookbook - 5.4. Wrapping a C library in Python with ctypes
The ctypes module takes care of the data type conversions between C and Python. In addition, the numpy.ctypeslib module provides facilities to use NumPy arrays wherever data buffers are used in the external library.
๐ŸŒ
Stav Shamir
stavshamir.github.io โ€บ python โ€บ making-your-c-library-callable-from-python-by-wrapping-it-with-cython
Making your C library callable from Python by wrapping it with Cython - Stav Shamir
May 3, 2018 - Cython is known for its ability to increase the performance of Python code. Another useful feature of Cython is making existing C functions callable from within (seemingly) pure Python modules.
๐ŸŒ
Real Python
realpython.com โ€บ python-bindings-overview
Python Bindings: Calling C or C++ From Python โ€“ Real Python
August 17, 2023 - Are you a Python developer with a C or C++ library youโ€™d like to use from Python? If so, then Python bindings allow you to call functions and pass data from Python to C or C++, letting you take advantage of the strengths of both languages.
๐ŸŒ
TutorialEdge
tutorialedge.net โ€บ python โ€บ python-c-extensions-tutorial
Creating Basic Python C Extensions - Tutorial | TutorialEdge.net
Letโ€™s dive into the C code. Open up the .c file that will contain your new module and add #include <Python.h> to the top.
๐ŸŒ
GitHub
github.com โ€บ cython โ€บ cython
GitHub - cython/cython: The most widely used Python to C compiler ยท GitHub
Cython translates Python code to C/C++ code, but additionally supports calling C functions and declaring C types on variables and class attributes. This allows broad to fine-grained manual tuning that lets the compiler generate very efficient C code from Cython code. This makes Cython the ideal language for wrapping external C libraries, and for fast C modules that speed up the execution of Python code.
Starred by 10.7K users
Forked by 1.6K users
Languages ย  Cython 51.6% | Python 41.9% | C 6.2% | C++ 0.2% | Shell 0.1% | Starlark 0.0%
๐ŸŒ
Quora
quora.com โ€บ What-is-the-best-way-to-use-a-Python-library-in-C
What is the best way to use a Python library in C++? - Quora
Answer: In order to use a Python library in C++, I have created a child process running Python and used inter-process communication to pass requests down to the child process and results back to the parent. In other situations where the requirement seemed to be using a Python library from C++, I...