Python’s magic method
__delattr__() implements the built-in
delattr() function that takes an object and an attribute name as arguments and removes the attribute from the object.
We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.
Syntax and Example
Let’s have a look at an example where you override the
__delattr__ magic method of a custom class Car to change the
speed attribute value to
42 instead of deleting it:
class Car: def __init__(self): self.speed = 100 def __delattr__(self, attr): self.speed = 42 # Create object porsche = Car() print(porsche.speed) # 100 delattr(porsche, 'speed') print(porsche.speed) # 42
Note that if you wouldn’t have overridden the
__delattr__() magic method, Python would’ve removed the
speed attribute which would’ve led to an error. You can see this scenario next in our recap on the built-in
delattr() function takes an object and an attribute name as arguments and removes the attribute from the object.
delattr(object, 'attribute') is semantically identical to
Before we dive into a practical example of the
delattr() function, feel free to watch my explainer video here:
Let’s dive into an example next.
First, create a
Car object with one attribute
# Define class with one attribute class Car: def __init__(self): self.speed = 100 # Create object porsche = Car()
Next, print the attribute
# What's the value for attribute speed? print(porsche.speed) # 100
delattr(porsche, speed) to remove the attribute
speed from the object
# Remove the attribute speed from porsche delattr(porsche, 'speed')
After removing the attribute, it cannot be accessed anymore:
# 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 <module> print(porsche.speed) AttributeError: 'Car' object has no attribute 'speed' '''
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.