{"id":473,"date":"2020-10-12T11:35:20","date_gmt":"2020-10-12T11:35:20","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=473"},"modified":"2025-03-31T10:09:17","modified_gmt":"2025-03-31T10:09:17","slug":"python-properties","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-properties\/","title":{"rendered":"Python Property"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>property<\/code> class and how to use it to define properties for a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-class-properties'>Introduction to class properties <a href=\"#introduction-to-class-properties\" class=\"anchor\" id=\"introduction-to-class-properties\" title=\"Anchor for Introduction to class properties\">#<\/a><\/h2>\n\n\n\n<p>The following defines a <code>Person<\/code> <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a> that has two <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\">attributes<\/a> <code>name<\/code> and <code>age<\/code>, and create a new instance of the <code>Person<\/code> class:<\/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\"><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, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n\njohn = Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">18<\/span>)<\/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>Since <code>age<\/code> is the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">instance attribute<\/a> of the <code>Person<\/code> class, you can assign it a new value like this:<\/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\">john.age = <span class=\"hljs-number\">19<\/span><\/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>The following assignment is also technically valid:<\/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\">john.age = <span class=\"hljs-number\">-1<\/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>However, the age is semantically incorrect. <\/p>\n\n\n\n<p>To ensure that the age is not zero or negative, you use the <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/\">if<\/a><\/code> statement to add a check as follows:<\/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\">age = <span class=\"hljs-number\">-1<\/span>\n<span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span>:\n    <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The age must be positive'<\/span>)\n<span class=\"hljs-keyword\">else<\/span>:\n    john.age = age<\/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<p>And you need to do this every time you want to assign a value to the <code>age<\/code> attribute. This is repetitive and difficult to maintain.<\/p>\n\n\n\n<p>To avoid this repetition, you can define a pair of methods called getter and setter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getter-and-setter'>Getter and setter <a href=\"#getter-and-setter\" class=\"anchor\" id=\"getter-and-setter\" title=\"Anchor for Getter and setter\">#<\/a><\/h2>\n\n\n\n<p>The getter and setter methods provide an interface for accessing an instance attribute:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The getter returns the value of an attribute<\/li>\n\n\n\n<li>The setter sets a new value for an attribute<\/li>\n<\/ul>\n\n\n\n<p>In our example, you can make the <code>age<\/code> attribute <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-private-attributes\/\">private<\/a> (by convention) and define a getter and a setter to manipulate the <code>age<\/code> attribute.<\/p>\n\n\n\n<p>The following shows the new <code>Person<\/code> class with a getter and setter for the <code>age<\/code> attribute:<\/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, age)<\/span>:<\/span>\n        self.name = name\n        self.set_age(age)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_age<\/span><span class=\"hljs-params\">(self, age)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The age must be positive'<\/span>)\n        self._age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_age<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self._age<\/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>In the <code>Person<\/code> class, the <code>set_age()<\/code> is the setter and the <code>get_age()<\/code> is the getter. By convention the getter and setter have the following name: <code>get_&lt;attribute&gt;()<\/code> and <code>set_&lt;attribute&gt;()<\/code>.<\/p>\n\n\n\n<p>In the <code>set_age()<\/code> method, we raise a <code>ValueError<\/code> if the <code>age<\/code> is less than or equal to zero. Otherwise, we assign the <code>age<\/code> argument to the <code>_age<\/code> attribute:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_age<\/span><span class=\"hljs-params\">(self, age)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span>:\n        <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The age must be positive'<\/span>)\n    self._age = age<\/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>The <code>get_age()<\/code> method returns the value of the <code>_age<\/code> attribute:<\/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\">get_age<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> self._age<\/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>In the <code>__init__()<\/code> method, we call the <code>set_age()<\/code> setter method to initialize the <code>_age<\/code> attribute:<\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n    self.name = name\n    self.set_age(age)<\/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>The following attempts to assign an invalid value to the <code>age<\/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\">john = Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">18<\/span>)\njohn.set_age(<span class=\"hljs-number\">-19<\/span>)<\/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>And Python issued a <code>ValueError<\/code> as expected.<\/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\">ValueError: The age must be positive<\/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<p>This code works just fine. But it has a backward compatibility issue.<\/p>\n\n\n\n<p>Suppose you released the <code>Person<\/code> class for a while and other developers have been already using it. And now you add the getter and setter, all the code that uses the Person won&#8217;t work anymore. <\/p>\n\n\n\n<p>To define a getter and setter method while achieving backward compatibility, you can use the <code>property()<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-python-property-class'>The Python property class <a href=\"#the-python-property-class\" class=\"anchor\" id=\"the-python-property-class\" title=\"Anchor for The Python property class\">#<\/a><\/h2>\n\n\n\n<p>The property class returns a <code>property<\/code> object. The <code>property()<\/code> class has the following syntax:<\/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\">property(fget=<span class=\"hljs-literal\">None<\/span>, fset=<span class=\"hljs-literal\">None<\/span>, fdel=<span class=\"hljs-literal\">None<\/span>, doc=<span class=\"hljs-literal\">None<\/span>)<\/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>The <code>property()<\/code> has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fget<\/code> is a function to get the value of the attribute, or the getter method.<\/li>\n\n\n\n<li><code>fset<\/code> is a function to set the value of the attribute, or the setter method.<\/li>\n\n\n\n<li><code>fdel<\/code> is a function to delete the attribute.<\/li>\n\n\n\n<li><code>doc<\/code> is a docstring i.e., a comment.<\/li>\n<\/ul>\n\n\n\n<p>The following uses the <code>property()<\/code> function to define the <code>age<\/code> property for the <code>Person<\/code> class.<\/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\"><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, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_age<\/span><span class=\"hljs-params\">(self, age)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The age must be positive'<\/span>)\n        self._age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_age<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self._age\n\n    age = property(fget=get_age, fset=set_age)<\/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>In the <code>Person<\/code> class, we create a new property object by calling the <code>property()<\/code> and assign the property object to the age attribute. Note that the <code>age<\/code> is a <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\">class attribute<\/a>, not an <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">instance attribute<\/a>. <\/p>\n\n\n\n<p>The following shows that the <code>Person.age<\/code> is a <code>property<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(Person.age)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&lt;property object at <span class=\"hljs-number\">0x000001F5F5149180<\/span>&gt;<\/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>The following creates a new instance of the <code>Person<\/code> class and access the <code>age<\/code> attribute:<\/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\">john = Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">18<\/span>)<\/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 <code>john.__dict__<\/code> stores the instance attributes of the john object. The following shows the contents of the <code>john.__dict__<\/code> :<\/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\">print(john.__dict__)<\/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<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{<span class=\"hljs-string\">'_age'<\/span>: <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'John'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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>As you can see clearly from the output, the <code>john.__dict__<\/code> doesn&#8217;t have the <code>age<\/code> attribute.<\/p>\n\n\n\n<p>The following assigns a value to the <code>age<\/code> attribute of the john object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">john.age = <span class=\"hljs-number\">19<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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 case, Python looks up the <code>age<\/code> attribute in the <code>john.__dict__<\/code> first. Because Python doesn&#8217;t find the <code>age<\/code> attribute in the <code>john.__dict__<\/code>, it&#8217;ll then find the <code>age<\/code> attribute in the <code>Person.__dict__<\/code>. <\/p>\n\n\n\n<p>The <code>Person.__dict__<\/code> stores the class attributes of the Person class. The following shows the contents of the <code>Person.__dict__<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mappingproxy({<span class=\"hljs-string\">'__dict__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__dict__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'__doc__'<\/span>: <span class=\"hljs-literal\">None<\/span>,\n              <span class=\"hljs-string\">'__init__'<\/span>: &lt;function Person.__init__ at <span class=\"hljs-number\">0x000002242F5B2670<\/span>&gt;,\n              <span class=\"hljs-string\">'__module__'<\/span>: <span class=\"hljs-string\">'__main__'<\/span>,\n              <span class=\"hljs-string\">'__weakref__'<\/span>: &lt;attribute <span class=\"hljs-string\">'__weakref__'<\/span> of <span class=\"hljs-string\">'Person'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'age'<\/span>: &lt;property object at <span class=\"hljs-number\">0x000002242EE39180<\/span>&gt;,\n              <span class=\"hljs-string\">'get_age'<\/span>: &lt;function Person.get_age at <span class=\"hljs-number\">0x000002242F5B2790<\/span>&gt;,\n              <span class=\"hljs-string\">'set_age'<\/span>: &lt;function Person.set_age at <span class=\"hljs-number\">0x000002242F5B2700<\/span>&gt;})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><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>Because Python finds the <code>age<\/code> attribute in the <code>Person.__dict__<\/code>, it&#8217;ll call the <code>age<\/code> property object.<\/p>\n\n\n\n<p>When you assign a value to the <code>age<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">john.age = <span class=\"hljs-number\">19<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><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>Python will call the function assigned to the <code>fset<\/code> argument, which is the <code>set_age()<\/code>. <\/p>\n\n\n\n<p>Similarly, when you read from the <code>age<\/code> property object, Python will execute the function assigned to the <code>fget<\/code> argument, which is the <code>get_age()<\/code> method.<\/p>\n\n\n\n<p>By using the <code>property()<\/code> class, we can add a property to a class while maintaining backward compatibility. In practice, you will define the attributes first. Later, you can add the property to the class if needed.<\/p>\n\n\n\n<p>Putting it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> pprint <span class=\"hljs-keyword\">import<\/span> pprint\n\n\n<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, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_age<\/span><span class=\"hljs-params\">(self, age)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The age must be positive'<\/span>)\n        self._age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_age<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self._age\n\n    age = property(fget=get_age, fset=set_age)\n\n\nprint(Person.age)\n\njohn = Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">18<\/span>)\npprint(john.__dict__)\n\njohn.age = <span class=\"hljs-number\">19<\/span>\npprint(Person.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBwcHJpbnQgaW1wb3J0IHBwcmludAoKCmNsYXNzIFBlcnNvbjoKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBuYW1lLCBhZ2UpOgogICAgICAgIHNlbGYubmFtZSA9IG5hbWUKICAgICAgICBzZWxmLmFnZSA9IGFnZQoKICAgIGRlZiBzZXRfYWdlKHNlbGYsIGFnZSk6CiAgICAgICAgaWYgYWdlIDw9IDA6CiAgICAgICAgICAgIHJhaXNlIFZhbHVlRXJyb3IoJ1RoZSBhZ2UgbXVzdCBiZSBwb3NpdGl2ZScpCiAgICAgICAgc2VsZi5fYWdlID0gYWdlCgogICAgZGVmIGdldF9hZ2Uoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuX2FnZQoKICAgIGFnZSA9IHByb3BlcnR5KGZnZXQ9Z2V0X2FnZSwgZnNldD1zZXRfYWdlKQoKCnByaW50KFBlcnNvbi5hZ2UpCgpqb2huID0gUGVyc29uKCdKb2huJywgMTgpCnBwcmludChqb2huLl9fZGljdF9fKQoKam9obi5hZ2UgPSAxOQpwcHJpbnQoUGVyc29uLl9fZGljdF9fKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\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\">\n<li>Use the Python <code>property()<\/code> class to define a property for a class.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=property\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\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=\"473\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-properties\/\"\n\t\t\t\tdata-post-title=\"Python Property\"\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=\"473\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-properties\/\"\n\t\t\t\tdata-post-title=\"Python Property\"\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&#8217;ll learn about the Python properties and how to use them effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-473","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/473","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=473"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/473\/revisions"}],"predecessor-version":[{"id":7309,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/473\/revisions\/7309"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}