{"id":5274,"date":"2020-04-28T10:47:40","date_gmt":"2020-04-28T10:47:40","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5274"},"modified":"2022-08-06T13:13:38","modified_gmt":"2022-08-06T13:13:38","slug":"python-vars-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-vars-method","title":{"rendered":"Python vars() &#8211; Find the __dict__ attribute"},"content":{"rendered":"\n<p>Hello everyone! In this article, we&#8217;ll look at using the Python <strong>vars()<\/strong> function, which returns the <code>__dict__<\/code> attribute of an object.<\/p>\n\n\n\n<p>This is rare function, but it helps to see how you can use it, as it is useful in certain situations. Let&#8217;s look at these situations now, using illustrative examples!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Python vars()<\/h2>\n\n\n\n<p>This function takes an object <code>obj<\/code>, and is of the form:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvars(&#x5B;obj])\n<\/pre><\/div>\n\n\n<p>This returns the <code>__dict__<\/code> attribute of <code>obj<\/code>, which contains all the attributes of the object which can be writable.<\/p>\n\n\n\n<p>Here, <code>obj<\/code> can be any module \/ class \/ instance of a class, etc.<\/p>\n\n\n\n<p>There are a couple of cases here, depending on the argument type and the number of arguments too.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If you don&#8217;t provide any arguments, Python <code>vars()<\/code> will act like the <code>locals()<\/code> method, which returns a dictionary which has the current local symbol table.<\/li><li>Since it returns the <code>__dict__<\/code> attribute, it the object does not have this attribute, it will raise a <code>TypeError<\/code> exception.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s now look at some examples, related to different objects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using Python <strong>vars<\/strong>() without any arguments<\/h2>\n\n\n\n<p>As mentioned earlier, this will act as the <code>locals()<\/code> method, and return a dictionary of the local symbol table.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(vars())\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&#039;__name__&#039;: &#039;__main__&#039;, &#039;__doc__&#039;: None, &#039;__package__&#039;: None, &#039;__loader__&#039;: &lt;_frozen_importlib_external.SourceFileLoader object at 0x108508390&gt;, &#039;__spec__&#039;: None, &#039;__annotations__&#039;: {}, &#039;__builtins__&#039;: &lt;module &#039;builtins&#039; (built-in)&gt;, &#039;__file__&#039;: &#039;\/Users\/vijay\/home\/python_vars_function.py&#039;, &#039;__cached__&#039;: None, &#039;Data&#039;: &lt;class &#039;__main__.Data&#039;&gt;, &#039;d&#039;: &lt;__main__.Data object at 0x108565048&gt;, &#039;math&#039;: &lt;module &#039;math&#039; from &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.6\/lib\/python3.6\/lib-dynload\/math.cpython-36m-darwin.so&#039;&gt;}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using Python vars() on a Class Object<\/h2>\n\n\n\n<p>If <code>obj<\/code> is a Class Type, or an instance of the class, let&#8217;s find out what <code>vars()<\/code> does in this case.<\/p>\n\n\n\n<p>Let&#8217;s create an example class <code>MyClass<\/code> and define some attributes on it&#8217;s <code>__init__()<\/code> method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __init__(self, i, j):\n        self.a = i\n        self.b = j\n\nm = MyClass(10, 20)\nprint(vars(m))\nprint(vars(MyClass))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&#039;a&#039;: 10, &#039;b&#039;: 20}\n{&#039;__module__&#039;: &#039;__main__&#039;, &#039;__init__&#039;: &lt;function MyClass.__init__ at 0x000001C24EA129D8&gt;, &#039;__dict__&#039;: &lt;attribute &#039;__dict__&#039; of &#039;MyClass&#039; objects&gt;, &#039;__weakref__&#039;: &lt;attribute &#039;__weakref__&#039; of &#039;MyClass&#039; objects&gt;, &#039;__doc__&#039;: None}\n<\/pre><\/div>\n\n\n<p>As you can observe, for the class instance, <code>vars()<\/code> returned all relevant attributes <code>a<\/code> and <code>b<\/code>, along with their values.<\/p>\n\n\n\n<p>Whereas in the case of the class <code>MyClass<\/code>, it is encapsulated under the <code>main<\/code> module, and has the <code>__init__<\/code> method, along with the <code>__dict__<\/code> attribute of the class.<\/p>\n\n\n\n<p><code>vars()<\/code> calls all of the dunder methods like <code>__repr__<\/code>, <code>__dict__<\/code>, etc.<\/p>\n\n\n\n<p>So, it is more convenient if you can call this function directly rather than calling the dunder methods. (Although there is no difference as such)<\/p>\n\n\n\n<p>Similarly, you can use <code>vars()<\/code> on other objects and classes, like <code>str<\/code> and <code>list<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(vars(str))\nprint(vars(list))\n<\/pre><\/div>\n\n\n<p>This will show all relevant attributes and instance methods for both classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using vars() on a module<\/h2>\n\n\n\n<p>We can also use this function on a module, to find out all it&#8217;s containing methods, along with other relevant information and even docstrings!<\/p>\n\n\n\n<p>For example, if you want to look at the built-in module <code>antigravity<\/code>, (which is an easter egg!) you do import it, and look at <code>vars(antigravity)<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport antigravity\n\nprint(vars(antigravity))\n<\/pre><\/div>\n\n\n<p><strong>Sample Output (Last few lines)<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n {&#039;__name__&#039;: &#039;antigravity&#039;, &#039;__doc__&#039;: None, &#039;__package__&#039;: &#039;&#039;, &#039;TimeoutError&#039;: &lt;class &#039;TimeoutError&#039;&gt;, &#039;open&#039;: &lt;built-in function open&gt;, &#039;quit&#039;: Use quit() or Ctrl-Z plus Return to exit, &#039;exit&#039;: Use exit() or Ctrl-Z plus Return to exit, &#039;copyright&#039;: Copyright (c) 2001-2018 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n}\n<\/pre><\/div>\n\n\n<p>If you use <code>vars()<\/code> on an object (like <code>int<\/code>) which does not have the <code>__dict__<\/code> attribute, it will raise a <code>TypeError<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(vars(12))\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTraceback (most recent call last):\n  File &quot;vars_example.py&quot;, line 12, in &lt;module&gt;\n    print(vars(12))\nTypeError: vars() argument must have __dict__ attribute\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we looked at the Python vars() function, which is useful if you want to quickly get the attributes and all representative methods of any Class\/Module\/object.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JournalDev article on Python vars() function<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this article, we&#8217;ll look at using the Python vars() function, which returns the __dict__ attribute of an object. This is rare function, but it helps to see how you can use it, as it is useful in certain situations. Let&#8217;s look at these situations now, using illustrative examples! Syntax of Python vars() [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":5280,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5274"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5274\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5280"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}