{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">import pprint<br\/><br\/>pprint.pprint(globals())<br\/>pprint.pprint(locals())<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def dump(obj):<br\/>  &#039;&#039;&#039;return a printable representation of an object for debugging&#039;&#039;&#039;<br\/>  newobj=obj<br\/>  if &#039;__dict__&#039; in dir(obj):<br\/>    newobj=obj.__dict__<br\/>    if &#039; object at &#039; in str(obj) and not newobj.has_key(&#039;__type__&#039;):<br\/>      newobj[&#039;__type__&#039;]=str(obj)<br\/>    for attr in newobj:<br\/>      newobj[attr]=dump(newobj[attr])<br\/>  return newobj<\/code><\/pre> <\/div>\n<ul>\n<li><strong>Here is the usage<\/strong><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">class stdClass(object): pass<br\/>obj=stdClass()<br\/>obj.int=1<br\/>obj.tup=(1,2,3,4)<br\/>obj.dict={&#039;a&#039;:1,&#039;b&#039;:2, &#039;c&#039;:3, &#039;more&#039;:{&#039;z&#039;:26,&#039;y&#039;:25}}<br\/>obj.list=[1,2,3,&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,[1,2,3,4]]<br\/>obj.subObj=stdClass()<br\/>obj.subObj.value=&#039;foobar&#039;<br\/>from pprint import pprint<br\/>pprint(dump(obj))<\/code><\/pre> <\/div>\n<p><strong>and the results<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">{<br\/>&#039;__type__&#039;: &#039;&lt;__main__.stdClass object at 0x2b126000b890&gt;&#039;,<br\/> &#039;dict&#039;: {&#039;a&#039;: 1, &#039;c&#039;: 3, &#039;b&#039;: 2, &#039;more&#039;: {&#039;y&#039;: 25, &#039;z&#039;: 26}},<br\/> &#039;int&#039;: 1,<br\/> &#039;list&#039;: [1, 2, 3, &#039;a&#039;, &#039;b&#039;, &#039;c&#039;, [1, 2, 3, 4]],<br\/> &#039;subObj&#039;: {&#039;__type__&#039;: &#039;&lt;__main__.stdClass object at 0x2b126000b8d0&gt;&#039;,<br\/>            &#039;value&#039;: &#039;foobar&#039;},<br\/> &#039;tup&#039;: (1, 2, 3, 4)<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">pip install var_dump<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">       def __init__ (self, PrintableClass):<br\/>           for name in dir(PrintableClass):<br\/>               value = getattr(PrintableClass,name)<br\/>               if  &#039;_&#039; not in str(name).join(str(value)):<br\/>                    print &#039;  .%s: %r&#039; % (name, value)<\/code><\/pre> <\/div>\n<ul>\n<li><strong>The sample of usage:<\/strong><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">Printer(MyClass)<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">from inspect import getmembers<br\/>from pprint import pprint<br\/>pprint(getmembers(yourObj))<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">print vars(foo),vars(bar)<\/code><\/pre> <\/div>\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}]}}