In Python is there any debug function equivalent to print_r
or var_dump
of PHP?
For example, in PHP:
$valor = 'Hello';
var_dump($valor); string(5)'Hello'
In Python would have some similar function for debug?
In Python is there any debug function equivalent to print_r
or var_dump
of PHP?
For example, in PHP:
$valor = 'Hello';
var_dump($valor); string(5)'Hello'
In Python would have some similar function for debug?
repr (Python 2) and reprlib (Python 3)
p>Produces a string representation of an object passed as parameter.
Produce formatted output for the data.
Example (source: pymotw.com ):
from pprint import pprint
from pprint_data import data
print 'PRINT:'
print data
print
print 'PPRINT:'
pprint(data)
$ python pprint_pprint.py
PRINT:
[(0, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (1, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (2, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'})]
PPRINT:
[(0,
{'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H'}),
(1,
{'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H'}),
(2,
{'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H'})]
These PHP functions are output / writing on screen functions, where var_dump has the peculiarity of displaying information about the variable.
To get a good approximation of the var_dump function, you can use both Python and Python > 3:
print(vars(a))
As noted by @Miguel, vars () only works if "a" has attribute dict , which is a dictionary or mapping of an object that stores attributes of that object, if it is integer or string, eg , no longer gives.
You can use Python's debugger > :
#!python3
'''
Exemplo de uso do debugger do Python
'''
#Importa a biblioteca PDB
import pdb
#Inicie seu programa
objeto = "maçã"
#Ponto onde o debugger vai começar a exibir informação sobre o programa
pdb.set_trace()
sujeito = "Mauricio"
verbo = "gosta"
frase = sujeito + " " + verbo + " " + objeto
print(frase)