{"id":122518,"date":"2020-12-25T09:32:30","date_gmt":"2020-12-25T09:32:30","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=19473"},"modified":"2020-12-25T09:32:30","modified_gmt":"2020-12-25T09:32:30","slug":"python-delattr","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/25\/python-delattr\/","title":{"rendered":"Python delattr()"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s built-in <code>delattr()<\/code> function takes an object and an attribute name as arguments and removes the attribute from the object. The call <code>delattr(object, 'attribute')<\/code> is semantically identical to <code>del object.attribute<\/code>. <\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python delattr() and del -- Finally Understanding These Built-in Functions\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/X-FbPBc0Sn8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>This article shows you how to use Python&#8217;s built-in <code>delattr()<\/code> function.<\/p>\n<h2>Usage<\/h2>\n<p>Learn by example! Here&#8217;s an example on how to use the <code>delattr()<\/code> <a href=\"https:\/\/blog.finxter.com\/python-built-in-functions\/\" title=\"Python Built-In Functions\">built-in function<\/a>.<\/p>\n<p>Create a <code>Car<\/code> object with one attribute <code>speed<\/code>. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Define class with one attribute\nclass Car: def __init__(self): self.speed = 100 # Create object\nporsche = Car()<\/pre>\n<p><a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Print Function [And Its SECRET Separator &amp; End Arguments]\">Print <\/a>the attribute <code>speed<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># What's the value for attribute speed?\nprint(porsche.speed)\n# 100<\/pre>\n<p>Use <code>delattr(porsche, speed)<\/code> to remove the attribute <code>speed<\/code> from the object <code>porsche<\/code>. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Remove the attribute speed from porsche\ndelattr(porsche, 'speed')<\/pre>\n<p>After removing the attribute, it cannot be accessed anymore:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Does this still work?\nprint(porsche.speed)\n# No: '''\nTraceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\HowToConvertBooleanToStringPython\\code.py\", line 18, in &lt;module> print(porsche.speed)\nAttributeError: 'Car' object has no attribute 'speed' '''<\/pre>\n<\/p>\n<h2>Syntax delattr()<\/h2>\n<p>The <code>delattr()<\/code> object has the following syntax: <\/p>\n<pre class=\"wp-block-preformatted\"><strong>Syntax: <\/strong>\n<code><strong>delattr(object, attribute)<\/strong> # Removes attribute from object<\/code><\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Arguments<\/strong><\/td>\n<td><code>object<\/code><\/td>\n<td>The object from which the attribute should be removed<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>string<\/code><\/td>\n<td>The attribute to be removed<\/td>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>None<\/code><\/td>\n<td>Returns Nothing. If the attribute doesn&#8217;t exist, the method does nothing.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Interactive Shell Exercise: Understanding delattr()<\/h2>\n<p>Consider the following interactive code:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/4045c3a543\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"356\" frameborder=\"0\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: <\/em>Does the code work? If yes, run it! If not, fix the bug!<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>But before we move on, I&#8217;m excited to present you my brand-new Python book <a rel=\"noreferrer noopener\" href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" title=\"https:\/\/amzn.to\/2WAYeJE\">Python One-Liners<\/a><\/strong> (Amazon Link).<\/p>\n<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.<\/strong> But it&#8217;s also an <strong>introduction to computer science<\/strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!<\/em><\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" width=\"215\" height=\"283\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/02\/image-1.png\" alt=\"\" class=\"wp-image-5969\"\/><\/a><\/figure>\n<\/div>\n<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). <\/p>\n<p>Link: <a href=\"https:\/\/nostarch.com\/pythononeliners\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/nostarch.com\/pythononeliners<\/a><\/p>\n<hr class=\"wp-block-separator\"\/>\n<h2>Python del vs delattr()<\/h2>\n<p>The alternative to Python&#8217;s built-in <code>delattr()<\/code> is to use the <a href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know\"><code>del<\/code> keyword<\/a> that is also built-in. <\/p>\n<p>The <code>delattr(object, 'attribute')<\/code> is semantically identical to the <code>del object.attribute<\/code> call. Note that in the first case, the attribute is given as a string, while in the second case, the attribute is given as a normal attribute name. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Define class with one attribute\nclass Car: def __init__(self): self.speed = 100 # Create object\nporsche = Car() # What's the value for attribute speed?\nprint(porsche.speed) # Remove the attribute speed from porsche\ndel porsche.speed # Does this still work?\nprint(porsche.speed)\n<\/pre>\n<p>The output is the same:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">100\nTraceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\HowToConvertBooleanToStringPython\\code.py\", line 17, in &lt;module> print(porsche.speed)\nAttributeError: 'Car' object has no attribute 'speed'<\/pre>\n<h2>Related Functions<\/h2>\n<ul>\n<li>The <code>getattr()<\/code> function returns the value of an attribute.<\/li>\n<li>The <code>hasattr()<\/code> function checks if an attribute exists.<\/li>\n<li>The <code>setattr()<\/code> function sets the value of an attribute.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>Python&#8217;s built-in <code>delattr()<\/code> function takes an object and an attribute name as arguments and removes the attribute from the object.<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p>I hope you enjoyed the article! To improve your Python education, you may want to join the popular free <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Email Academy\">Finxter Email Academy<\/a>:<\/p>\n<p>Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<\/p>\n<\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-delattr\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python delattr()<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s built-in delattr() function takes an object and an attribute name as arguments and removes the attribute from the object. The call delattr(object, &#8216;attribute&#8217;) is semantically identical to del object.attribute. This article shows you how to use Python&#8217;s built-in delattr() function. Usage Learn by example! Here&#8217;s an example on how to use the delattr() built-in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-122518","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=122518"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122518\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=122518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=122518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=122518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}