How to make something like PHP in Python. Part 2

var_dump implementation

Fullstack CTO
2 min readFeb 1, 2023

If you have recently retrained from a PHP programmer to a Python developer, you may have difficulties at first from a different mainsheet and feature set. I’ll make your job easier. I’ll show you how to implement an alternative function for dumping variables with registration in global scope.

I am not going to tell you that you can print, pprint and dir. I’ll show you how to implement a function and make its behavior similar to PHP.

The easiest way to do something similar is to write a function like this

import builtins

def var_dump(expression):
print(f"{type(expression)}: {repr(expression)}")

# Register in global scope
builtins.var_dump = var_dump

If you include this code in the main file of your application, the function will be available globally and you won’t have to import it every time.

Let’s test it

But if you want something more like this, here’s an option for you

I will be adding different versions of the code to this repository. If you are interested, please like it and subscribe to updates.

--

--

Fullstack CTO
Fullstack CTO

Written by Fullstack CTO

CTO and co-founder at NEWHR & Geekjob

No responses yet