Recently in the Web development of Python (forgive my changeable, good things always want to learn ...) node. JS is the same, but in the process there are always some problems, whether it is web.py or Django, developed really useless PHP convenient, after all, the existence of a short time, a lot of imperfect place.
For example, I am debugging PHP's most commonly used functions, var_dump, in Python can not find a suitable replacement function. PHP Var_dump is a particularly useful function that can output the value of any variable, whether you are an object or an array, or just a number. It can always be exported in a friendly way, and I often need to look at the variable information in a certain location when I debug it, it is convenient to call it:
But there's no such a good alternative when developing python.
Previously thought of repr, but this function just called the object in the __str__, and direct print obj no difference. Print is printing it, and repr is returning it as a value. If the class to which the object belongs does not have a function defined __str__, then the return is an ugly string of characters.
Then I thought of the VARs function, the VARs function is a python built-in function that is specifically used to output the internal information of an object. However, this object must have a __dict__ function in the class it belongs to. General classes have this dict, but objects such as [] and {} Do not have this dict, so calling the VARs function throws an exception:
Copy the Code code as follows:
Traceback (most recent):
File " ", line 1, in
Typeerror:vars () argument must have __dict__ attribute
So after several search, find a better, function can be similar to Var_dump function as follows:
Copy the Code code as follows:
def dump (obj):
' Return a printable representation of an object for debugging '
Newobj=obj
If ' __dict__ ' in Dir (obj):
newobj=obj.__dict__
If ' object at ' in str (obj) and not Newobj.has_key (' __type__ '):
newobj[' __type__ ']=str (obj)
For attr in newobj:
Newobj[attr]=dump (Newobj[attr])
Return newobj
Here's how to use it:
Copy the Code code as follows:
Class StdClass (object): Pass
Obj=stdclass ()
Obj.int=1
obj.tup= (1,2,3,4)
Obj.dict={' A ': 1, ' B ': 2, ' C ': 3, ' more ': {' Z ': +, ' y ': 25}}
obj.list=[1,2,3, ' A ', ' B ', ' C ', [1,2,3,4]]
Obj.subobj=stdclass ()
Obj.subobj.value= ' Foobar '
From Pprint import Pprint
Pprint (Dump (obj))
The final output is:
Copy the Code code as follows:
{' __type__ ': ' <__main__.stdclass object at 0x2b126000b890> ',
' Dict ': {' A ': 1, ' C ': 3, ' B ': 2, ' more ': {' y ': +, ' Z ': 26}},
' int ': 1,
' List ': [1, 2, 3, ' A ', ' B ', ' C ', [1, 2, 3, 4]],
' Subobj ': {' __type__ ': ' <__main__.stdclass object at 0x2b126000b8d0> ',
' Value ': ' Foobar '},
' Tup ': (1, 2, 3, 4)}
Then GitHub has an open-source module, which you can refer to: https://github.com/sha256/python-var-dump
Speaking of Pprint This function, he is a human output function, will output the content in the way programmers like to output on the screen. See this article for a better understanding: http://www.jb51.net/article/60143.htm