Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python delattr()

#1
Python delattr()

<div><p class="has-pale-cyan-blue-background-color has-background">Python’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>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<p>This article shows you how to use Python’s built-in <code>delattr()</code> function.</p>
<h2>Usage</h2>
<p>Learn by example! Here’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>
<p>Create a <code>Car</code> object with one attribute <code>speed</code>. </p>
<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
class Car: def __init__(self): self.speed = 100 # Create object
porsche = Car()</pre>
<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>
<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?
print(porsche.speed)
# 100</pre>
<p>Use <code>delattr(porsche, speed)</code> to remove the attribute <code>speed</code> from the object <code>porsche</code>. </p>
<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
delattr(porsche, 'speed')</pre>
<p>After removing the attribute, it cannot be accessed anymore:</p>
<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?
print(porsche.speed)
# No: '''
Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 18, in &lt;module> print(porsche.speed)
AttributeError: 'Car' object has no attribute 'speed' '''</pre>
</p>
<h2>Syntax delattr()</h2>
<p>The <code>delattr()</code> object has the following syntax: </p>
<pre class="wp-block-preformatted"><strong>Syntax: </strong>
<code><strong>delattr(object, attribute)</strong> # Removes attribute from object</code></pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Arguments</strong></td>
<td><code>object</code></td>
<td>The object from which the attribute should be removed</td>
</tr>
<tr>
<td></td>
<td><code>string</code></td>
<td>The attribute to be removed</td>
</tr>
<tr>
<td><strong>Return Value</strong></td>
<td><code>None</code></td>
<td>Returns Nothing. If the attribute doesn’t exist, the method does nothing.</td>
</tr>
</tbody>
</table>
</figure>
<h2>Interactive Shell Exercise: Understanding delattr()</h2>
<p>Consider the following interactive code:</p>
<p> <iframe src="https://trinket.io/embed/python/4045c3a543" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="356" frameborder="0"></iframe> </p>
<p><em><strong>Exercise</strong>: </em>Does the code work? If yes, run it! If not, fix the bug!</p>
<hr class="wp-block-separator"/>
<p><strong>But before we move on, I’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>
<p>If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it’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>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img 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>
</div>
<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>
<p>Link: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>
<hr class="wp-block-separator"/>
<h2>Python del vs delattr()</h2>
<p>The alternative to Python’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>
<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>
<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
class Car: def __init__(self): self.speed = 100 # Create object
porsche = Car() # What's the value for attribute speed?
print(porsche.speed) # Remove the attribute speed from porsche
del porsche.speed # Does this still work?
print(porsche.speed)
</pre>
<p>The output is the same:</p>
<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
Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 17, in &lt;module> print(porsche.speed)
AttributeError: 'Car' object has no attribute 'speed'</pre>
<h2>Related Functions</h2>
<ul>
<li>The <code>getattr()</code> function returns the value of an attribute.</li>
<li>The <code>hasattr()</code> function checks if an attribute exists.</li>
<li>The <code>setattr()</code> function sets the value of an attribute.</li>
</ul>
<h2>Summary</h2>
<p>Python’s built-in <code>delattr()</code> function takes an object and an attribute name as arguments and removes the attribute from the object.</p>
<hr class="wp-block-separator"/>
<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>
<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>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</p>
</p>
<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>
</div>


https://www.sickgaming.net/blog/2020/12/...n-delattr/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016