{"id":170,"date":"2024-01-25T14:18:11","date_gmt":"2024-01-25T14:18:11","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=170"},"modified":"2024-01-25T14:18:12","modified_gmt":"2024-01-25T14:18:12","slug":"python-delete-property","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-delete-property\/","title":{"rendered":"Python Delete Property"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the property() class to delete the property of an object.<\/p>\n\n\n\n<p>To create a\u00a0property\u00a0of a class, you use the\u00a0@property decorator. Underhood, the\u00a0<code>@property<\/code>\u00a0decorator uses the\u00a0<code>property<\/code>\u00a0class that has three methods: setter, getter, and deleter.<\/p>\n\n\n\n<p>By using the deleter, you can delete a property from an object. Notice that the\u00a0<code>deleter()<\/code>\u00a0method deletes a property of an object, not a\u00a0class.<\/p>\n\n\n\n<p>The following defines the\u00a0<code>Person<\/code>\u00a0class with the\u00a0<code>name<\/code>\u00a0property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from pprint import pprint class Person: def __init__(self, name): self._name = name @property def name(self): return self._name @name.setter def name(self, value): if value.strip() == '': raise ValueError('name cannot be empty') self._name = value @name.deleter def name(self): del self._name<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In the&nbsp;<code>Person<\/code>&nbsp;class, we use the&nbsp;<code>@name.deleter<\/code>&nbsp;decorator. Inside the deleter, we use the&nbsp;<code>del<\/code>&nbsp;keyword to delete the&nbsp;<code>_name<\/code>&nbsp;attribute of the&nbsp;<code>Person<\/code>&nbsp;instance.<\/p>\n\n\n\n<p>The following shows the\u00a0<code>__dict__<\/code>\u00a0of the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pprint(Person.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mappingproxy({'__dict__': &lt;attribute '__dict__' of 'Person' objects>, '__doc__': None, '__init__': &lt;function Person.__init__ at 0x000001DC41D62670>, '__module__': '__main__', '__weakref__': &lt;attribute '__weakref__' of 'Person' objects>, 'name': &lt;property object at 0x000001DC41C89130>})<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person.__dict__<\/code>\u00a0class has the\u00a0<code>name<\/code>\u00a0variable. The following creates a new instance of the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>person.__dict__<\/code>\u00a0has the\u00a0<code>_name<\/code>\u00a0attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pprint(person.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'_name': 'John'}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following uses the\u00a0<code>del<\/code>\u00a0keyword to delete the\u00a0<code>name<\/code>\u00a0property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>del person.name<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Internally, Python will execute the\u00a0<code>deleter<\/code>\u00a0method that deletes the\u00a0<code>_name<\/code>\u00a0attribute from the\u00a0<code>person<\/code>\u00a0object. The\u00a0<code>person.__dict__<\/code>\u00a0will be empty like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And if you attempt to access\u00a0<code>name<\/code>\u00a0property again, you\u2019ll get an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(person.name)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>AttributeError: 'Person' object has no attribute '_name'<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the property() class to delete the property of an object. To create a\u00a0property\u00a0of a class, you use the\u00a0@property decorator. Underhood, the\u00a0@property\u00a0decorator uses the\u00a0property\u00a0class that has three methods: setter, getter, and deleter. By using the deleter, you can delete a property from an object. Notice that the\u00a0deleter()\u00a0method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-3-property"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/170\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}