Python delattr()

Python’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, 'attribute') is semantically identical to del object.attribute.

This article shows you how to use Python’s built-in delattr() function.

Usage

Learn by example! Here’s an example on how to use the delattr() built-in function.

Create a Car object with one attribute speed.

# Define class with one attribute
class Car:
    def __init__(self):
        self.speed = 100


# Create object
porsche = Car()

Print the attribute speed:

# What's the value for attribute speed?
print(porsche.speed)
# 100

Use delattr(porsche, speed) to remove the attribute speed from the object porsche.

# 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'
'''

Syntax delattr()

The delattr() object has the following syntax:

Syntax: 
delattr(object, attribute)         # Removes attribute from object
ArgumentsobjectThe object from which the attribute should be removed
stringThe attribute to be removed
Return ValueNoneReturns 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!


Check out 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).

Publisher Link: https://nostarch.com/pythononeliners


Python del vs delattr()

The alternative to Python’s built-in delattr() is to use the del keyword that is also built-in.

The 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'

Related Functions

  • The getattr() function returns the value of an attribute.
  • The setattr() function changes the value of an attribute.
  • The hasattr() function checks if an attribute exists.
  • The delattr() function deletes an existing attribute.

Summary

Python’s built-in 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.

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.

Join the free webinar now!