In this post I will be showing you how to add, remove, and check for class attributes in Python.
For this article I will be using the class below as our class we are going to be adding and removing attributes from.
class MyClass: def __init__(self): self.attrib1 = 'attribute1' self.attrib2 = 'attribute2' def method_one(self): return 'this is method one'
Taking a quick look through this code we can see that it has 2 string attributes and one method.
Checking if an object has an attribute
To check to see if an object has a particular attribute you just need to create an instance of that object and then use the 'hasattr' method passing in the instance of your object, and a string that corresponds to the name of the attribute you are checking for.
If you add the code below to the bottom of the file we can see the output of the 'hasattr' method when it looks for an attribute that does not exist.
if __name__ == '__main__': t = MyClass() print(hasattr(t, 'mynewattr'))
Looking at line 3 we can see that we create an instance of "MyClass" and store that in the variable "t" and then line 4 is where we are checking to see if the class as an attribute 'mynewattr' and printing out the result.
So at this point we are ready to run the file to see what it does so go ahead, save and run the file and you will get the output below.
$ python example.py False
So we can now tell that our object does not have an attribute 'mynewattr' so if we change the code under the if statement to the code snippet below checking for an attribute that exists we will instead get an output of True.
if __name__ == '__main__': t = MyClass() print(hasattr(t, 'attrib1'))
You can see that the hasattr method still takes a string argument but now it corresponds to an attribute that does exist.
Now when we run that code we get the following output.
$ python example.py True
Adding an attribute to an Object
Lets say that we need to add an attribute to our object we can do this with the 'setattr' method. The 'setattr' method takes 3 arguments the first is the object you would like to add the attribute to, the second is the name of the attribute you are adding, and the third is the value of the attribute.
If you replace the if statement at the bottom of your file from the previous example above with the code below we can see the 'setattr' method in action.
if __name__ == '__main__': t = MyClass() print(hasattr(t, 'mynewattr')) setattr(t, 'mynewattr', 'this is the value') print(hasattr(t, 'mynewattr')) print(t.mynewattr)
We can see lines 1-4 are the same as in the previous example but on line 5 we are adding the previously non existant attribute 'mynewattr' to the object instance 't' and giving it a string value of 'this is the value'. Line 6 we check to see if it has an attribute again and lastly we print out our newly added attribute.
If we run our code now we will get the following output
$ python example.py False True this is the value $
and as expected our first print statement was 'False' because our attribute didn't exist yet, our second was 'True' after we created, and our third and final print statement was able to successfully print out the contents of our newly created attribute
Deleting an attribute
Lets say now that we would like to delete an attribute, this can be done with the 'delattr' method by providing it the object you wish to delete the attribute from and the string representation of the attribute you wish to delete.
If you once again replace the code at the bottom of your file with the snippet below we will see the 'delattr' method in action.
if __name__ == '__main__': t = MyClass() print(hasattr(t, 'attrib1')) delattr(t, 'attrib1') print(hasattr(t, 'attrib1'))
Again the first 4 lines we should be familiar with from the previous examples but in line 5 we can see that we are using the 'delattr' method to remove the attribute self.attrib1 from 't' and then to demonstrate proof that the attribute was deleted we are checking to see if the object has an attribute of 'attrib1'.
If we save and run our code again we will get the following output.
$ python example.py True False $
As expected our first print statement was 'True' because 'attrib1' existed and then our second print statement was false showing we successfully deleted 'attrib1' from the instance of MyClass in 't'.
I hope this article was able to help demonstrate adding, removing, and checking attributes of objects in Python. If there is anything missing or unclear please feel free to leave a comment and I will get back to you. If this article was helpful for you please give a share to help others!