Python hasattr() method Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return true if present, else false. In this article, we will see how to check if an object has an attribute in Python. Syntax of hasattr() function Syntax : hasattr(obj, key) Parameters : obj : The object whose which attribute has to be checked.key : Attribute which needs to be checked. Returns : Returns True, if attribute is present else returns False. Example 1: Python hasattr() function example Here we will check if an object has an attribute, to find attributes of the object in python we have demonstrated the following code. Python3 # declaring class class GfG: name = "GeeksforGeeks" age = 24 # initializing object obj = GfG() # using hasattr() to check name print("Does name exist ? " + str(hasattr(obj, 'name'))) # using hasattr() to check motto print("Does motto exist ? " + str(hasattr(obj, 'motto'))) Output: Does name exist ? True Does motto exist ? FalseExample 2: Performance analysis between hasattr() method and try statement This is the Simple Ways to Check if an Object has Attribute in Python or not using performance analysis between hasattr() function and try statement. Python3 import time # declaring class class GfG: name = "GeeksforGeeks" age = 24 # initializing object obj = GfG() # use of hasattr to check motto start_hasattr = time.time() if(hasattr(obj, 'motto')): print("Motto is there") else: print("No Motto") print("Time to execute hasattr : " + str(time.time() - start_hasattr)) # use of try/except to check motto start_try = time.time() try: print(obj.motto) print("Motto is there") except AttributeError: print("No Motto") print("Time to execute try : " + str(time.time() - start_try)) Output: No Motto Time to execute hasattr : 5.245208740234375e-06 No Motto Time to execute try : 2.6226043701171875e-06 Result: Conventional try/except takes lesser time than hasattr(), but for readability of code, hasattr() is always a better choice. Applications: This function can be used to check keys to avoid unnecessary errors in case of accessing absent keys. Chaining of hasattr() is used sometimes to avoid entry of one associated attribute if the other is not present. Comment More infoAdvertise with us Next Article Python List methods M manjeet_04 Follow Improve Article Tags : Python Python-Built-in-functions Python-OOP python-object Practice Tags : python Similar Reads Python | getattr() method The getattr() method in Python returns the value of a named attribute of an object. If the attribute is not found then it returns the default value provided. If no default is given and the attribute does not exist then it raises an AttributeError.Python getattr() Method SyntaxSyntax : getattr(obj, k 3 min read Python iter() method Python iter() method is a built-in method that allows to create an iterator from an iterable. An iterator is an object that enables us to traverse through a collection of items one element at a time. Letâs start by understanding how iter() works with a simple example.Pythona = [10, 20, 30, 40] # Con 2 min read Python List methods Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example.List MethodsLet's look at different list methods in Python:append(): Adds an 3 min read Python __add__() magic method Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects. It implements the addition operator "+" in Python. Python __add__() Syntax Syntax: obj1.__add__(self, obj2) obj1: First object to add in the second object.obj2 1 min read Python String lstrip() Method The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.Let's take an example to remove whitespace from the starting of a string.Pythons = " Hello Python!" res = s.lstrip() print(res)OutputHell 2 min read Instance method in Python A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to 2 min read Like