5
In Python there is some function for debug 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?
5
In Python there is some function for debug 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?
3
pr (Python 2) and reprlib (Python 3)
Produces a string-like representation of a passed object such as parameter.
Produces formatted data output.
Example (source: pymotw.with):
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 functions of output/writing on screen, whereas var_dump has the peculiarity to show information about the variable.
To get a good approximation of the function var_dump, you can use so on Python 2.7.* and Python 3:
print(vars(a))
As noted by @Miguel, vars() only works if "a" has an attribute Dict, which is a dictionary or mapping of an object that stores attributes of that object, if it is integer or string, e. g., it is no longer possible.
You can use the Debugger python (PDB):
#!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)
I think it only works if the a
have attribute __dict__
, if it is integer or string for ex no longer gives
I tried here and made that mistake: TypeError: vars() argument must have __dict__ attribute
There are a few things you could explain better, and there must be ways to order it, look here: https://www.google.pt/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=var%20dump%20python . You can count on my +1 if the answer is extended and improved
I will do that yes. There are still other functions I want to put there. Only I’m at work now rsrss.
Browser other questions tagged python debug
You are not signed in. Login or sign up in order to post.
Take a look at this library debug.
– gato