Embedding source code in your Medium stories is pretty easy. Here’s the end result:

Intro to Python

You’re looking at the Python computer language. The code above defines a “type of object” in this case a Python. The three indented methods, each with a strange “__rib__” looking name, define python behaviors: birth, eating, representing.

For more about Python, see my previous story here on Medium.

More advanced Python looks like this (explanation below):

Rational Number class

Like in the Python example, the code defines a new type and its several behaviors. “Rat” means not the animal, but “rational number” i.e. a “fraction” with a numerator and denominator.

Python has a Fraction type in the Standard Library (from fractions import Fraction) so rolling your own is not really necessary, unless perhaps your goal is to learn Python. If you already know how fractions work, when added together, multiplied, divided, raised to powers, then the above code serves to elucidate Python.

For example, to “divide” (__truediv__) is to “multiply by the inverse” where the inverse is a fraction raised to the -1 power (__pow__).

Here’s another (similar) implementation on Repl.it, with runnable code.

For Further Reading:

Number Theory: School of Tomorrow (Jupyter Notebook)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store