Python implements the var_dump function of php and pythonvar_dump

Source: Internet
Author: User

Python implements the var_dump function of php and pythonvar_dump

Recently I am working on python web development (forgive me for being changeable, and I always want to learn everything... Node. js), but there are always some problems in the process, whether it is web. py is still django, and it is really useless to develop php. After all, there is a short time and many imperfections.

For example, var_dump, the most common function in php debugging, cannot find an appropriate replacement function in python. In php, var_dump is a particularly useful function that can output the value of any variable, whether it is an object, an array, or a number. It can always be output in a friendly way. During debugging, I often need to view the variable information at a certain position. It is very convenient to call it:

However, there is no good alternative when developing python.

Previously I thought of repr, but this function only calls _ str __in the object, which is no different from direct print obj. Print is to print it, And repr is to return it as a value. If the class to which the object belongs does not define the _ str _ function, the returned result will be an ugly string of characters.

Later I Came Up With The vars function. The vars function is a python built-in function that is used to output the internal information of an object. However, the class to which this object belongs must have the _ dict _ function. This dict exists in common classes, but the dict does not exist in objects such as [] and {}. Therefore, an exception will be thrown when The vars function is called:

Copy codeThe Code is as follows:
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: vars () argument must have _ dict _ attribute

So after several queries, it is better to find one by one. functions similar to var_dump are as follows:

Copy codeThe Code is 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

This method is used:
Copy codeThe Code is 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': 26, 'y': 25 }}
Obj. list = [1, 2, 3, 'A', 'B', 'C', [1, 2, 4]
Obj. subObj = stdClass ()
Obj. subObj. value = 'foobar'

From pprint import pprint
Pprint (dump (obj ))

The final output is:
Copy codeThe Code is as follows:
{'_ Type _': '<__ main _. stdClass object at 0x2b126000b890> ',
'Dict ': {'A': 1, 'C': 3,' B ': 2, 'more': {'y': 25, '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, you can refer to: https://github.com/sha256/python-var-dump

Let's talk about the pprint function. It is a user-friendly output function that will output the content on the screen in a way that programmers like. See this article for better understanding: http://www.bkjia.com/article/60143.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.