Recently doing the web development of Python (forgive my changeable, good things always want to learn ... Node.js is also), but the process has always encountered some problems, whether it is web.py or Django, developed does not use PHP convenient, after all, the existence of a relatively short time, a lot of imperfect places.
For example, I'm debugging PHP's most commonly used function, var_dump, in Python can not find a suitable alternative function. Var_dump in PHP 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 used in a friendly way, and I often need to look at the variable information in a location when I debug it, so it's convenient to call it:
But there is no good alternative to developing python.
Thought of repr before, but this function just called the __str__ in the object, and the direct print obj is no different. Print prints it, and repr returns it as a value. If the class to which the object belongs does not have the __STR__ function defined, then the return will be an ugly string of characters.
Then came the thought of the VARs function, the VARs function, which is a Python built-in function designed to output the internal information of an object. However, the class in which this object belongs must have a __dict__ function. General classes have this dict, but objects such as [] and {} Do not have this dict, so calling the VARs function throws an exception:
Copy Code code as follows:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Typeerror:vars () argument must have __dict__ attribute
So after looking, find a better, function can be similar to Var_dump functions as follows:
Copy Code code as follows:
def dump (obj):
"Return a printable representation of the An object for debugging '"
Newobj=obj
If ' __dict__ ' in Dir (obj):
newobj=obj.__dict__
If ' in ' in str (obj) and not Newobj.has_key (' __type__ '):
newobj[' __type__ ']=str (obj)
For attr in newobj:
Newobj[attr]=dump (Newobj[attr])
Return newobj
This is the way to use:
Copy 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 ': num, ' 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 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 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 better understanding: http://www.jb51.net/article/60143.htm