{"id":4732,"date":"2022-09-12T09:07:47","date_gmt":"2022-09-12T09:07:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4732"},"modified":"2022-10-02T00:36:37","modified_gmt":"2022-10-02T00:36:37","slug":"python-setattr","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-setattr\/","title":{"rendered":"Python setattr"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Python <code>setattr()<\/code> function to set a value to an attribute of an object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-setattr-function'>Introduction to the Python setattr() function <a href=\"#introduction-to-the-python-setattr-function\" class=\"anchor\" id=\"introduction-to-the-python-setattr-function\" title=\"Anchor for Introduction to the Python setattr() function\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>setattr()<\/code><\/code> function sets a <code>value<\/code> to an attribute specified by the string <code>name<\/code> of an <code>object<\/code>. The following shows the syntax of the <code><code>setattr()<\/code><\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">setattr(object, name, value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>setattr()<\/code> function has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>object<\/code> is the object that you want to set the attribute.<\/li><li><code>name<\/code> is the name of the attribute that you want to set a value.<\/li><li><code>value<\/code> is the value to set.<\/li><\/ul>\n\n\n\n<p>In practice, you&#8217;ll use the <code><code>setattr()<\/code><\/code> function in metal programming. Please check out the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass\/\">Meta class<\/a> and <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass-example\/\">its example<\/a> for the practical usage of the <code><code>setattr()<\/code><\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-setattr-function-examples'>Python setattr() function examples <a href=\"#python-setattr-function-examples\" class=\"anchor\" id=\"python-setattr-function-examples\" title=\"Anchor for Python setattr() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>setattr()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-python-setattr-to-set-a-value-to-an-attribute-of-an-object'>1) Using Python setattr() to set a value to an attribute of an object <a href=\"#1-using-python-setattr-to-set-a-value-to-an-attribute-of-an-object\" class=\"anchor\" id=\"1-using-python-setattr-to-set-a-value-to-an-attribute-of-an-object\" title=\"Anchor for 1) Using Python setattr() to set a value to an attribute of an object\">#<\/a><\/h3>\n\n\n\n<p>The following example defines a <code>Person<\/code> class and uses the <code>setattr()<\/code> to assign a value to the <code>age<\/code> attribute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n\n\nperson = Person(<span class=\"hljs-string\">'John'<\/span>)\nsetattr(person, <span class=\"hljs-string\">'age'<\/span>, <span class=\"hljs-number\">22<\/span>)\n\nprint(person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'age'<\/span>: <span class=\"hljs-number\">22<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>Person<\/code> class doesn&#8217;t have the <code>age<\/code> attribute. The <code>setattr()<\/code> adds the <code>age<\/code> attribute to the person object and sets its values to 22.<\/p>\n\n\n\n<p>Note that the <code>setattr()<\/code> only changes an individual <code>person<\/code> object, not the <code>Person<\/code> class. If you create another instance of the Person class, this object won&#8217;t have the age attribute. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n\n\nperson = Person(<span class=\"hljs-string\">'John'<\/span>)\nsetattr(person, <span class=\"hljs-string\">'age'<\/span>, <span class=\"hljs-number\">22<\/span>)\nprint(person.__dict__)  <span class=\"hljs-comment\"># {'name': 'John', 'age': 22}<\/span>\n\nperson2 = Person(<span class=\"hljs-string\">'Jane'<\/span>)\nprint(person2.__dict__)  <span class=\"hljs-comment\"># {'name': 'Jane'}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-python-setattr-to-assign-a-method-to-an-object'>2) Using Python setattr() to assign a method to an object <a href=\"#2-using-python-setattr-to-assign-a-method-to-an-object\" class=\"anchor\" id=\"2-using-python-setattr-to-assign-a-method-to-an-object\" title=\"Anchor for 2) Using Python setattr() to assign a method to an object\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>setattr()<\/code> function to assign a function to an object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_hi<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hi'<\/span>\n\n\nperson = Person(<span class=\"hljs-string\">'John'<\/span>)\nsetattr(person, <span class=\"hljs-string\">'greeting'<\/span>, say_hi)\nprint(person.greeting())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define the <code>say_hi()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_hi<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hi'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, create a new <code>Person<\/code> object and assign the <code>say_hi<\/code> function to the <code>greeting<\/code> attribute of the <code>person<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = Person(<span class=\"hljs-string\">'John'<\/span>)\nsetattr(person, <span class=\"hljs-string\">'greeting'<\/span>, say_hi)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, call the <code>say_hi()<\/code> function via the <code>greeting()<\/code> attribute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(person.greeting())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Hi<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-setattr-to-convert-a-dictionary-to-an-object'>Using setattr() to convert a dictionary to an object <a href=\"#using-setattr-to-convert-a-dictionary-to-an-object\" class=\"anchor\" id=\"using-setattr-to-convert-a-dictionary-to-an-object\" title=\"Anchor for Using setattr() to convert a dictionary to an object\">#<\/a><\/h2>\n\n\n\n<p>When you work with a dictionary, especially one that has a lot of nested dictionaries, you need to use square brackets with keys surrounded by a string. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person_dict = {\r\n    <span class=\"hljs-string\">'first_name'<\/span>: <span class=\"hljs-string\">'John'<\/span>,\r\n    <span class=\"hljs-string\">'last_name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>,\r\n    <span class=\"hljs-string\">'skills'<\/span>: &#91;<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'Tkinter'<\/span>, <span class=\"hljs-string\">'Django'<\/span>],\r\n    <span class=\"hljs-string\">'address'<\/span>: {\r\n        <span class=\"hljs-string\">'house_no'<\/span>: <span class=\"hljs-number\">999<\/span>,\r\n        <span class=\"hljs-string\">'street'<\/span>: <span class=\"hljs-string\">'Fox Avenue'<\/span>,\r\n        <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'San Jose'<\/span>,\r\n        <span class=\"hljs-string\">'state'<\/span>: <span class=\"hljs-string\">'CA'<\/span>,\r\n        <span class=\"hljs-string\">'zipcode'<\/span>: <span class=\"hljs-number\">95134<\/span>,\r\n        <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">'USA'<\/span>\r\n    }\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the value of first name of the <code>person_dict<\/code> dictionary, you need to use the <code>'first_name'<\/code> string as a key:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person_dict&#91;<span class=\"hljs-string\">'first_name'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the value of the state of the <code>person_dict<\/code>, you need to access the address first and then use the <code>'state'<\/code> string as a key:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">person_dict<\/span><span class=\"hljs-selector-attr\">&#91;<span class=\"hljs-string\">'address'<\/span>]<\/span><span class=\"hljs-selector-attr\">&#91;<span class=\"hljs-string\">'state'<\/span>]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It would be more convenient to access the state like an object like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person_dict.address.state<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To do that, you need to convert the dictionary to an object. The key of each key\/value pair in the dictionary is the attribute of the object. Also, you need to deal with the nested dictionaries.<\/p>\n\n\n\n<p>The following <code>DictToObject<\/code> class uses the <code>setattr()<\/code> function and recursive technique to convert a dictionary to an object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">DictToObject<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, d)<\/span>:<\/span>\r\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> isinstance(d, dict):\r\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The argument d must be a dictionary object'<\/span>)\r\n\r\n        <span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> d.items():\r\n            <span class=\"hljs-keyword\">if<\/span> isinstance(value, (list, tuple)):\r\n                setattr(self, key, &#91;DictToObject(v) <span class=\"hljs-keyword\">if<\/span> isinstance(v, dict)\r\n                                    <span class=\"hljs-keyword\">else<\/span> v <span class=\"hljs-keyword\">for<\/span> v <span class=\"hljs-keyword\">in<\/span> value])\r\n            <span class=\"hljs-keyword\">else<\/span>:\r\n                setattr(self, key, DictToObject(value)\r\n                        <span class=\"hljs-keyword\">if<\/span> isinstance(value, dict) <span class=\"hljs-keyword\">else<\/span> value)\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows how to convert the <code>person_dict<\/code> to the <code>person<\/code> object using the <code>DictToObject<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">DictToObject<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, d)<\/span>:<\/span>\r\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> isinstance(d, dict):\r\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The argument d must be a dictionary object'<\/span>)\r\n\r\n        <span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> d.items():\r\n            <span class=\"hljs-keyword\">if<\/span> isinstance(value, (list, tuple)):\r\n                setattr(self, key, &#91;DictToObject(v) <span class=\"hljs-keyword\">if<\/span> isinstance(v, dict)\r\n                                    <span class=\"hljs-keyword\">else<\/span> v <span class=\"hljs-keyword\">for<\/span> v <span class=\"hljs-keyword\">in<\/span> value])\r\n            <span class=\"hljs-keyword\">else<\/span>:\r\n                setattr(self, key, DictToObject(value)\r\n                        <span class=\"hljs-keyword\">if<\/span> isinstance(value, dict) <span class=\"hljs-keyword\">else<\/span> value)\r\n\r\n\r\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\r\n    person_dict = {\r\n        <span class=\"hljs-string\">'first_name'<\/span>: <span class=\"hljs-string\">'John'<\/span>,\r\n        <span class=\"hljs-string\">'last_name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>,\r\n        <span class=\"hljs-string\">'skills'<\/span>: &#91;<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'Tkinter'<\/span>, <span class=\"hljs-string\">'Django'<\/span>],\r\n        <span class=\"hljs-string\">'address'<\/span>: {\r\n            <span class=\"hljs-string\">'house_no'<\/span>: <span class=\"hljs-number\">999<\/span>,\r\n            <span class=\"hljs-string\">'street'<\/span>: <span class=\"hljs-string\">'Fox Avenue'<\/span>,\r\n            <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'San Jose'<\/span>,\r\n            <span class=\"hljs-string\">'state'<\/span>: <span class=\"hljs-string\">'CA'<\/span>,\r\n            <span class=\"hljs-string\">'zipcode'<\/span>: <span class=\"hljs-number\">95134<\/span>,\r\n            <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">'USA'<\/span>\r\n        }\r\n    }\r\n\r\n    person = DictToObject(person_dict)\r\n    print(person.first_name)  <span class=\"hljs-comment\"># John<\/span>\r\n    print(person.address.state)  <span class=\"hljs-comment\"># CA<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use Python <code>setattr()<\/code> function to set a value to an attribute of an object.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4732\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-setattr\/\"\n\t\t\t\tdata-post-title=\"Python setattr\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4732\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-setattr\/\"\n\t\t\t\tdata-post-title=\"Python setattr\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Python setattr() function to set a value to an attribute of an object.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1055,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4732","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4732"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4732\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1055"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}