{"id":1945,"date":"2017-03-23T19:42:07","date_gmt":"2017-03-23T14:12:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1945"},"modified":"2018-10-30T17:16:50","modified_gmt":"2018-10-30T11:46:50","slug":"python-equivalent-phps-var_dump","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-equivalent-phps-var_dump\/","title":{"rendered":"What is a Python equivalent of PHP&#8217;s var_dump()"},"content":{"rendered":"<h2 id=\"problem\"><span style=\"color: #ff0000;\"><label class=\"label label-warning\">PROBLEM:<\/label><\/span><\/h2>\n<ul>\n<li>When debugging in PHP, we frequently find it useful to simply stick a <strong>var_dump()<\/strong> in our code to show me what a variable is, what its value is, and the same for anything that it contains.<\/li>\n<li>What is a good <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-program-to-find-volume-surface-area-of-a-cylinder\" target=\"_blank\" rel=\"noopener\">Python<\/a> equivalent for this?<\/li>\n<\/ul>\n<p>When debugging in <a href=\"https:\/\/www.wikitechy.com\/php\/php-functions\" target=\"_blank\" rel=\"noopener\">PHP<\/a>, we frequently find it useful to simply <strong>stick a var_dump()<\/strong> in our code to show me what a variable is, what its value is, and the same for anything that it contains.<\/p>\n<h3 id=\"solution-1\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 1:<\/label><\/span><\/h3>\n<ul>\n<li>To display a value, you can use the <strong>pprint module<\/strong>. The easiest way to dump all variables with it is to do<\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dimport%20pprint%0A%0Apprint.pprint(globals())%0Apprint.pprint(locals())%0A\u201d message=\u201dpython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>If you are running in <strong>CGI<\/strong>, a useful <a href=\"https:\/\/www.wikitechy.com\/tutorials\/apache-pig\/apache-pig-tutorial\/how-to-debug-a-pig-script.php\" target=\"_blank\" rel=\"noopener\">debugging<\/a> feature is the <strong>cgitb module<\/strong>, which displays the value of local variables as part of the traceback.<\/li>\n<\/ul>\n<h3 id=\"solution-2\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 2:<\/label><\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201ddef%20dump(obj)%3A%0A%20%20\u201d\u2019return%20a%20printable%20representation%20of%20an%20object%20for%20debugging\u201d\u2019%0A%20%20newobj%3Dobj%0A%20%20if%20\u2019__dict__\u2019%20in%20dir(obj)%3A%0A%20%20%20%20newobj%3Dobj.__dict__%0A%20%20%20%20if%20\u2019%20object%20at%20\u2019%20in%20str(obj)%20and%20not%20newobj.has_key(\u2018__type__\u2019)%3A%0A%20%20%20%20%20%20newobj%5B\u2019__type__\u2019%5D%3Dstr(obj)%0A%20%20%20%20for%20attr%20in%20newobj%3A%0A%20%20%20%20%20%20newobj%5Battr%5D%3Ddump(newobj%5Battr%5D)%0A%20%20return%20newobj%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li><strong>Here is the usage<\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dclass%20stdClass(object)%3A%20pass%0Aobj%3DstdClass()%0Aobj.int%3D1%0Aobj.tup%3D(1%2C2%2C3%2C4)%0Aobj.dict%3D%7B\u2019a\u2019%3A1%2C\u2019b\u2019%3A2%2C%20\u2019c\u2019%3A3%2C%20\u2019more\u2019%3A%7B\u2019z\u2019%3A26%2C\u2019y\u2019%3A25%7D%7D%0Aobj.list%3D%5B1%2C2%2C3%2C\u2019a\u2019%2C\u2019b\u2019%2C\u2019c\u2019%2C%5B1%2C2%2C3%2C4%5D%5D%0Aobj.subObj%3DstdClass()%0Aobj.subObj.value%3D\u2019foobar\u2019%0Afrom%20pprint%20import%20pprint%0Apprint(dump(obj))%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>and the results<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%7B%0A\u2019__type__\u2019%3A%20\u2019%3C__main__.stdClass%20object%20at%200x2b126000b890%3E\u2019%2C%0A%20\u2019dict\u2019%3A%20%7B\u2019a\u2019%3A%201%2C%20\u2019c\u2019%3A%203%2C%20\u2019b\u2019%3A%202%2C%20\u2019more\u2019%3A%20%7B\u2019y\u2019%3A%2025%2C%20\u2019z\u2019%3A%2026%7D%7D%2C%0A%20\u2019int\u2019%3A%201%2C%0A%20\u2019list\u2019%3A%20%5B1%2C%202%2C%203%2C%20\u2019a\u2019%2C%20\u2019b\u2019%2C%20\u2019c\u2019%2C%20%5B1%2C%202%2C%203%2C%204%5D%5D%2C%0A%20\u2019subObj\u2019%3A%20%7B\u2019__type__\u2019%3A%20\u2019%3C__main__.stdClass%20object%20at%200x2b126000b8d0%3E\u2019%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20\u2019value\u2019%3A%20\u2019foobar\u2019%7D%2C%0A%20\u2019tup\u2019%3A%20(1%2C%202%2C%203%2C%204)%0A%7D%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"solution-3\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 3:<\/label><\/span><\/h3>\n<ul>\n<li>You can simply install it using <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.wikitechy.com\/technology\/get-ipads-pip-mode-iphone\/\" target=\"_blank\" rel=\"noopener\"><strong>pip<\/strong><\/a><span style=\"color: #000000;\">:<\/span><\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dpip%20install%20var_dump%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-4\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 4:<\/label><\/span><\/h3>\n<ul>\n<li>Here we use self-written <strong>Printer class<\/strong>, but <strong>dir()<\/strong> is also good for outputting the instance fields\/values.<\/li>\n<\/ul>\n<h4 id=\"class-printer\"><span style=\"color: #000080;\"><b>class Printer:<\/b><\/span><\/h4>\n[pastacode lang=\u201dpython\u201d manual=\u201d%20%20%20%20%20%20%20def%20__init__%20(self%2C%20PrintableClass)%3A%0A%20%20%20%20%20%20%20%20%20%20%20for%20name%20in%20dir(PrintableClass)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%20getattr(PrintableClass%2Cname)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20%20\u2019_\u2019%20not%20in%20str(name).join(str(value))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print%20\u2019%20%20.%25s%3A%20%25r\u2019%20%25%20(name%2C%20value)%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li><strong>The sample of usage:<\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dPrinter(MyClass)%0A\u201d message=\u201dPython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-5\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 5:<\/label><\/span><\/h3>\n<ul>\n<li>In PHP\u2019s var_dump() is <strong>pprint()<\/strong> with the<strong> getmembers() function<\/strong> in the built-in inspect module:<\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dfrom%20inspect%20import%20getmembers%0Afrom%20pprint%20import%20pprint%0Apprint(getmembers(yourObj))%0A\u201d message=\u201dpython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-6\"><span style=\"color: #003300;\"><label class=\"label label-info\">SOLUTION 6:<\/label><\/span><\/h3>\n<ul>\n<li>The best equivalent to <strong>PHP\u2019s var_dump($foo, $bar)<\/strong> is combine print with vars:<\/li>\n<\/ul>\n[pastacode lang=\u201dpython\u201d manual=\u201dprint%20vars(foo)%2Cvars(bar)%0A\u201d message=\u201dpython code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM: When debugging in PHP, we frequently find it useful to simply stick a var_dump() in our code to show me what a variable is, what its value is, and the same for anything that it contains. What is a good Python equivalent for this? When debugging in PHP, we frequently find it useful to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,4148],"tags":[4186,4187,4184,2830,404,4183,4181,4182,4185,4188],"class_list":["post-1945","post","type-post","status-publish","format-standard","hentry","category-php","category-python","tag-is-there-an-equivalent-of-phps-hash_hmac-in-pythondjango","tag-python-equivalent-for-phps-usort","tag-python-equivalent-of-phps-memory_get_usage","tag-reference-what-does-this-error-mean-in-php","tag-reference-what-does-this-symbol-mean-in-php","tag-what-does-the-yield-keyword-do-in-python","tag-what-is-a-metaclass-in-python","tag-what-is-the-difference-between-staticmethod-and-classmethod-in-python","tag-what-is-the-equivalent-of-phps-print_r-in-python","tag-what-is-the-python-equivalent-to-phps-_get"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1945"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1945\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}