Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

When debugging in PHP, I frequently find it useful to simply stick a var_dump() in my code to show me what a variable is, what its value is, and the same for anything that it contains.

What is a good Python equivalent for this?

2 Answers

0 votes
by (106k points)

The best Python equivalent of PHP's var_dump() is to combine print with vars:

print vars(foo),vars(bar)

Another important method if you want to display the value nicely, you can use the pprint module. The easiest way to dump all variables with it.

from pprint import pprint

pprint(globals())

pprint(locals())

0 votes
by (340 points)

PHP var_dump is used to get detailed information regarding variables their type and value. To achieve same in python we can use type() function to get the datatype of the variable.

Code:
def var_dump(var):
print(f” Type:{type(var}, Value:{var}”)

var_dump(23.02)
var_dump(‘hello’)

--Type: float, Value:23.02
--Type:string, Value:’hello’

Function call will display the type of the variable along with the value

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Karan Singh (4.1k points)
0 votes
1 answer
asked Sep 26, 2019 in Python by Karan Singh (4.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...