12-25-2020, 09:06 PM
Python delattr()
Python’s built-in function takes an object and an attribute name as arguments and removes the attribute from the object. The call is semantically identical to .
This article shows you how to use Python’s built-in function.
Usage
Learn by example! Here’s an example on how to use the built-in function.
Create a object with one attribute .
Printthe attribute:
Use to remove the attribute from the object .
After removing the attribute, it cannot be accessed anymore:
Syntax delattr()
The object has the following syntax:
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:
https://trinket.io/embed/python/4045c3a543
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 brand-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!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners
Python del vs delattr()
The alternative to Python’s built-in is to use the keyword that is also built-in.
The is semantically identical to the 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.
The output is the same:
Related Functions
Summary
Python’s built-in 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!
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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
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.
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.
Join the free webinar now!
The post Python delattr() first appeared on Finxter.
Python’s built-in
Code:
delattr()Code:
delattr(object, 'attribute')Code:
del object.attributeThis article shows you how to use Python’s built-in
Code:
delattr()Usage
Learn by example! Here’s an example on how to use the
Code:
delattr()Create a
Code:
CarCode:
speedCode:
# Define class with one attribute
class Car: def __init__(self): self.speed = 100 # Create object
porsche = Car()Printthe attribute
Code:
speedCode:
# What's the value for attribute speed?
print(porsche.speed)
# 100Use
Code:
delattr(porsche, speed)Code:
speedCode:
porscheCode:
# Remove the attribute speed from porsche
delattr(porsche, 'speed')After removing the attribute, it cannot be accessed anymore:
Code:
# 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
Code:
delattr()Code:
Syntax:
delattr(object, attribute) # Removes attribute from objectArguments |
Code:
objectThe object from which the attribute should be removed
|
Code:
stringThe attribute to be removed
Return Value |
Code:
NoneReturns Nothing. If the attribute doesn’t exist, the method does nothing.
Interactive Shell Exercise: Understanding delattr()
Consider the following interactive code:
https://trinket.io/embed/python/4045c3a543
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 brand-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!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners
Python del vs delattr()
The alternative to Python’s built-in
Code:
delattr()Code:
delThe
Code:
delattr(object, 'attribute')Code:
del object.attributeCode:
# 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:
Code:
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'- The function returns the value of an attribute.Code:
getattr()
- The function checks if an attribute exists.Code:
hasattr()
- The function sets the value of an attribute.Code:
setattr()
Summary
Python’s built-in
Code:
delattr()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!
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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
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.
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.
Join the free webinar now!
The post Python delattr() first appeared on Finxter.

