delattr() function takes an object and an attribute name as arguments and removes the attribute from the object. The call
delattr(object, 'attribute') is semantically identical to
This article shows you how to use Python’s built-in
Learn by example! Here’s an example on how to use the
delattr() built-in function.
Car object with one attribute
# Define class with one attribute class Car: def __init__(self): self.speed = 100 # Create object porsche = Car()
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' '''
delattr() object has the following syntax:
delattr(object, attribute) # Removes attribute from object
|Arguments||The object from which the attribute should be removed|
|The attribute to be removed|
|Return Value||Returns Nothing. If the attribute doesn’t exist, the method does nothing.|
Interactive Shell Exercise: Understanding delattr()
Consider the following interactive code:
Exercise: Does the code work? If yes, run it! If not, fix the bug!
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Python del vs delattr()
The alternative to Python’s built-in
delattr() is to use the
del keyword that is also built-in.
delattr(object, 'attribute') is semantically identical to the
del object.attribute 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.
# 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)
The output is the same:
100 Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 17, in <module> print(porsche.speed) AttributeError: 'Car' object has no attribute 'speed'
getattr()function returns the value of an attribute.
setattr()function changes the value of an attribute.
hasattr()function checks if an attribute exists.
delattr()function deletes an existing attribute.
delattr() function takes an object and an attribute name as arguments and removes the attribute from the object.
I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
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!
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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch 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.