TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Programming Languages / Python / Software Development

Python Under the Hood

This tutorial takes a beginner-friendly look at dynamic typing, strong typing, and the inner workings of the Python interpreter.
Sep 26th, 2024 12:00pm by
Featued image for: Python Under the Hood
Featured image via Unsplash+.

Python is a dynamic, strongly typed, interpreted language. These terms sound abstract and intimidating at first, but they’re easily understandable and contribute to why the language is, dare I say, “user-friendly.” Let’s break ‘em down and simplify.

Dynamic Language

Programming languages fall into one of two camps — dynamic or static. Static languages are C, C++, Java and Go. One of the defining characteristics of static languages is that variable types must be defined in the source code. Once the developer defines a variable as a certain data type ( i.e., if the cat variable is a string), it’s a string forever(rrrrr). Once the variable is defined at compile time, it can’t be changed at runtime.

With dynamic languages, the interpreter infers variable types at runtime (more on the interpreter and runtime later). This changes what we can do and can’t do, significantly. Dynamic languages include Python, JavaScript, Ruby, PHP.

This means Python variable assignments can be as simple as:

Python’s runtime will read Mooney and assign the appropriate memory without previously being told that Mooney is a string.

vs C++

Another characteristic of a dynamic language is the value’s ability to change — within the same data type and to a different data type without throwing an error.

The code above returns the following results
Mooney
Seth
[Seth, Mooney]

Strongly Typed

This refers to directly to memory and how it can or can’t be changed. Though values of Python values can change, they can’t change in unexpected ways without throwing errors. Here’s the best way to explain:

Integer + integer = success
String + string = success
Integer + string = error

You can change variable types but they need an explicit conversion.

The Python Interpreter

Python’s runtime makes this possible. Python’s runtime refers to the tools used to write and run Python code. The runtime is the software stack responsible for the installation and running of application code and dependencies. Runtimes include the command line, integrated development environments (IDEs) like VS Code or Jupyter Notebook, and web servers like Django and Flask. The runtime includes CPython — Python’s default bytecode interpreter. CPython’s main purpose is to read code written by (mostly) humans and turn it into machine-readable code.

CPython reads and interprets Python source code and executes line by line (hence the interpreted language). Before execution, CPython compiles the Python source code into bytecode. The Python Virtual Machine (PVM) is what executes the code. Since CPython handles Python code execution, it also manages Python memory management. This includes memory allocation and garbage collection. CPython provides a standard library that includes modules and packages for tasks such as I/O, networking, and data manipulation.

For a deeper understanding of how Python’s interpreter works under the hood, check out this video. Python’s under the hood tools allow for flexible and expressive programming. This makes it well suited for the tasks the language is known for such as scripting, web development, and data analysis.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.