0

I am learning Python and how to make classes. I was curious how the classes are made inside Python itself! For example, in datetime.py (I find it by googling) I was checking how they used __add__ or __sub__ which is using "if isinstance(other, timedelta):" that was an interesting learning. Also, I am learning what professionally written programs look like.

My question is how can I find the source codes of internal classes and functions inside Python, for example, I am interested to see how they implement add in print(), that can print(1+2) -> 3 and print('a'+'b') -> ab

Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23
Prober
  • 11
  • 2
  • The additions aren't implemented inside `print`. `1 + 2` is always 3, and `'a' + 'b'`is always '`'ab'`. – molbdnilo Mar 04 '22 at 18:09
  • 1
    I think this post might help you! https://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions – CrazyChaos Mar 04 '22 at 18:09

3 Answers3

2

The source code for the reference implementation of Python is available here at their GitHub mirror. However, it's worth noting that large parts of the Python language are implemented in C, including the core engine and many of the standard library libraries. Really understanding how everything is implemented under the hood requires a fair amount of C fluency.

Nick Bailey
  • 3,078
  • 2
  • 11
  • 13
  • Thanks Nick. Still I can not find the source codes. Can you please show me how to find the Python Source for print() function? – Prober Mar 04 '22 at 18:40
  • I don't know the Python source by heart, but I'm 85% sure the `print` built in function is implemented in C. You might have to do a fair amount of digging in the source to find it. – Nick Bailey Mar 04 '22 at 18:50
  • This is a helpful answer. I would add that viewing the source also requires an understanding of the CPython project and how things are organized. The code that is executed when one issues `print()` for example is in `builtinmodule.c` – Nathan Mar 04 '22 at 18:50
0

Python is an open source language which means the source code is available to any interested party. I would suggest looking at the source files on the machine you are using or looking at the CPython Github repo.

print() is a built in module. It is written in C and the source can be viewed in the file bltinmodule.c.

You may also find it useful to learn about the functions available in Python for getting help, like help() (documentation available here). To learn about the print() function you can call:

help(print)

I recommend reading the Beginner's Guide as a starting point for more resources.

Nathan
  • 3,082
  • 1
  • 27
  • 42
  • I have updated my answer with a pointer to the print source. If you want to find other functions you can use the search feature on Github or a searching tool on the source files on your local machine. – Nathan Mar 04 '22 at 18:47
  • How can I find it on my machine? I searched on my mac for "print.py" but not found. – Prober Mar 04 '22 at 18:49
  • Not all the functions are in single files. In order to search effectively you will need to gain some understanding of how the CPython project is organized. – Nathan Mar 04 '22 at 18:54
0

ipython is a great tool for exploring how things work. Just add "??" after a function or other callable, and it show the code when possible, ie. when it's "pure python".

Eg:

import this
this??
Philo
  • 169
  • 2
  • 4
  • Hello Philo, I have anaconda jupyter ipython and tried import print import print() they all give me errors can you guide me a bit more. thanks – Prober Mar 04 '22 at 18:31
  • print is a builtin function, implemented in C in the standard Python, so its source code is not available dynamically, with ipython or other ways of introspection. – Philo Mar 04 '22 at 19:00