Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2405
Abstract
id="1d65">Therefore, we still can’t totally ensure the value of <code>self._score</code> is always valid. Not to mention setting a value by a method is not as convenient as setting it directly. This is where the property decorator comes to rescue.</p><figure id="ea7e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*1ctMH1tnTE2_SyH2"><figcaption>Photo by <a href="https://unsplash.com/@mrcalvert?utm_source=medium&utm_medium=referral">Seb Mooze</a> on <a href="https://unsplash.com?utm_source=medium&utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="f05d">How to Use the Property Decorator?</h1><p id="036e">Let’s modify the <code>Student</code> class like this:</p>
<figure id="5000">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ZhouYang1993/ae48e81ce64f20cfc053b9bb58438349.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="7013">As the above code shown, after using the <code>@property</code> decorator, we can both set the value directly and ensure the validity of it. The property decorator is a very useful and widely used mechanism in Python classes. It helps us call the getter, setter and deleter methods directly by the attribute.</p><p id="3367">A common template for using the property decorator is:</p>
<figure id="e4a4">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ZhouYang1993/3754a398db9b1a76eecc963fa7a44517.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="94a4">Note: The names of getter, setter and deleter methods should be the same. The best choice is to use the attribute’s name.</p><h1 id="1cfd">Another Way to Use the Property Decorator</h1><p id="6ace">The property is just a build-in decorator in Python. Therefore, like other decorators, we can use it by another way:</p>
<figure id="a27a">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ZhouYang1993/01a4709990d3ebecca59bf9e51b1164c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
Options
</div>
</div>
</figure></iframe></div></div></figure><p id="5ff2"><i>The <code>property(fget=None,fset=None,fdel=None,doc=None)</code> function has four parameters:</i></p><ul><li><b><i>fget()</i></b><i> — used to get the attribute value</i></li><li><b><i>fset()</i></b><i> — used to set attribute value</i></li><li><b><i>fdel()</i></b><i> — used to delete the attribute value</i></li><li><b><i>doc()</i></b><i> — string that contains the documentation (docstring) for the attribute</i></li></ul><h1 id="a7f4">Define a Read-only Attribute</h1><p id="4a42">As we known, there are three methods (getter, setter and deleter) we can define by the property decorator. If one of the three methods is not defined, we cannot use this method. Therefore, we can skillfully use the property decorator to define a read-only attribute. For example:</p>
<figure id="23a1">
<div>
<div>
<iframe class="gist-iframe" src="/gist/ZhouYang1993/86c6c605c53dd8b669164524b91d65fa.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="36c7">As the above example shown, we don’t define the setter method of <code>score</code> , so <code>score</code> can’t be set. In other words, it’s a read-only attribute. This is an important usage scenario of the property decorator.</p><h1 id="9e79">Conclusion</h1><p id="e516">The property decorator is a powerful tool and widely used in Python classes. It allows us to write concise code while ensuring the necessary checks on parameters, so that the possibility of errors is reduced.</p><p id="fd74"><b><i>Thanks for reading. More Python tutorials:</i></b></p><div id="090b" class="link-block">
<a href="https://readmedium.com/the-ultimate-python-tutorial-keep-updating-13fa36cce44b">
<div>
<div>
<h2>The Ultimate Python Tutorial (Keep Updating)</h2>
<div><h3>The most valuable Python Tutorial you should save!</h3></div>
<div><p>medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*rIHlzv0kwEQj9m-g)"></div>
</div>
</div>
</a>
</div></article></body>