Home » Programming » Python » Python Hasattr

Python hasattr() – What it Does and How to Use It [Examples]

The hasattr() function is used to check whether an object has a given named attribute. Here is how it is used, with examples.

Python Objects and Classes

Python is an object oriented programming languageObject oriented means that rather than code being written as a list of instructions, accumulating data and passing the results to the following lines of code (known as procedural programming), data and functionality is contained within the objects it describes. Each object is a self-contained representation of something, that can be modified, and passed between functions in a non-procedural/non-linear manner.

class is a consistent kind of object, which defines the fixed attributes and methods used to interact with it. This is useful as if we know that all objects of a certain class contain the same data and behave the same way, we know what we can and can’t do with them elsewhere in the code.

So, for example, you might have a Car class, which has the attributes of makeyear, and colour. In Python, classes are defined like this:

class Car:
    make = "Ford"
    year = 2000
    colour = "red"

    def repaint(self):
        self.colour = "yellow"

Now, whenever we encounter an object of the class Car, we know exactly what the data inside will look like, regardless of the actual contents. We know that we can access a colour attribute and that that information will be present.

Object Attributes and Methods

Above, a Car class is defined, with some attributes (also known as properties) – the variables stored within it which are used to describe it (make, year, colour), and a method, a function contained within the class that can be used to interact with it – in this case the repaint() method changes the colour of the car.

The attributes shown above are the default values for the class. Once the class has been defined, objects can be instantiated (created) from that class. They will all share the same attributes and methods, and so can be treated in the same way, no matter where they appear in your code, or the differences of the values within the objects.

Using hasattr() to Check Whether an Object has an Attribute in Python

Below, a new car is created from the Car class defined earlier:

blueToyota = Car()

The hasattr() function can be used to check if an object has a given attribute:

hasattr(blueToyota, 'colour') # Returns TRUE

If the attribute is present, hasattr() will return TRUE.

hasattr(blueToyota, 'length') # Returns FALSE

If the attribute is not present, hasattr() will return FALSE.

hasattr() can also be used to check if the class itself has an attribute in its specification:

hasattr(Car, 'make') # Returns TRUE

Once we know that an object has an attribute, we can check the value using the getattr() function:

print(getattr(blueToyota, 'colour'))

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment