{"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=&#8221;python&#8221; manual=&#8221;import%20pprint%0A%0Apprint.pprint(globals())%0Apprint.pprint(locals())%0A&#8221; message=&#8221;python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\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=&#8221;python&#8221; manual=&#8221;def%20dump(obj)%3A%0A%20%20&#8221;&#8217;return%20a%20printable%20representation%20of%20an%20object%20for%20debugging&#8221;&#8217;%0A%20%20newobj%3Dobj%0A%20%20if%20&#8217;__dict__&#8217;%20in%20dir(obj)%3A%0A%20%20%20%20newobj%3Dobj.__dict__%0A%20%20%20%20if%20&#8217;%20object%20at%20&#8217;%20in%20str(obj)%20and%20not%20newobj.has_key(&#8216;__type__&#8217;)%3A%0A%20%20%20%20%20%20newobj%5B&#8217;__type__&#8217;%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&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li><strong>Here is the usage<\/strong><\/li>\n<\/ul>\n[pastacode lang=&#8221;python&#8221; manual=&#8221;class%20stdClass(object)%3A%20pass%0Aobj%3DstdClass()%0Aobj.int%3D1%0Aobj.tup%3D(1%2C2%2C3%2C4)%0Aobj.dict%3D%7B&#8217;a&#8217;%3A1%2C&#8217;b&#8217;%3A2%2C%20&#8217;c&#8217;%3A3%2C%20&#8217;more&#8217;%3A%7B&#8217;z&#8217;%3A26%2C&#8217;y&#8217;%3A25%7D%7D%0Aobj.list%3D%5B1%2C2%2C3%2C&#8217;a&#8217;%2C&#8217;b&#8217;%2C&#8217;c&#8217;%2C%5B1%2C2%2C3%2C4%5D%5D%0Aobj.subObj%3DstdClass()%0Aobj.subObj.value%3D&#8217;foobar&#8217;%0Afrom%20pprint%20import%20pprint%0Apprint(dump(obj))%0A&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><strong>and the results<\/strong><\/p>\n[pastacode lang=&#8221;python&#8221; manual=&#8221;%7B%0A&#8217;__type__&#8217;%3A%20&#8217;%3C__main__.stdClass%20object%20at%200x2b126000b890%3E&#8217;%2C%0A%20&#8217;dict&#8217;%3A%20%7B&#8217;a&#8217;%3A%201%2C%20&#8217;c&#8217;%3A%203%2C%20&#8217;b&#8217;%3A%202%2C%20&#8217;more&#8217;%3A%20%7B&#8217;y&#8217;%3A%2025%2C%20&#8217;z&#8217;%3A%2026%7D%7D%2C%0A%20&#8217;int&#8217;%3A%201%2C%0A%20&#8217;list&#8217;%3A%20%5B1%2C%202%2C%203%2C%20&#8217;a&#8217;%2C%20&#8217;b&#8217;%2C%20&#8217;c&#8217;%2C%20%5B1%2C%202%2C%203%2C%204%5D%5D%2C%0A%20&#8217;subObj&#8217;%3A%20%7B&#8217;__type__&#8217;%3A%20&#8217;%3C__main__.stdClass%20object%20at%200x2b126000b8d0%3E&#8217;%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20&#8217;value&#8217;%3A%20&#8217;foobar&#8217;%7D%2C%0A%20&#8217;tup&#8217;%3A%20(1%2C%202%2C%203%2C%204)%0A%7D%0A&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\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=&#8221;python&#8221; manual=&#8221;pip%20install%20var_dump%0A&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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=&#8221;python&#8221; manual=&#8221;%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&#8217;_&#8217;%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&#8217;%20%20.%25s%3A%20%25r&#8217;%20%25%20(name%2C%20value)%0A&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li><strong>The sample of usage:<\/strong><\/li>\n<\/ul>\n[pastacode lang=&#8221;python&#8221; manual=&#8221;Printer(MyClass)%0A&#8221; message=&#8221;Python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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&#8217;s var_dump() is <strong>pprint()<\/strong> with the<strong> getmembers() function<\/strong> in the built-in inspect module:<\/li>\n<\/ul>\n[pastacode lang=&#8221;python&#8221; manual=&#8221;from%20inspect%20import%20getmembers%0Afrom%20pprint%20import%20pprint%0Apprint(getmembers(yourObj))%0A&#8221; message=&#8221;python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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&#8217;s var_dump($foo, $bar)<\/strong> is combine print with vars:<\/li>\n<\/ul>\n[pastacode lang=&#8221;python&#8221; manual=&#8221;print%20vars(foo)%2Cvars(bar)%0A&#8221; message=&#8221;python code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\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}]}}