Menu Close

Python hasattr() Function

In this python built-in functions tutorial, we will go through hasattr() function to check a specified object has a specific attribute or not. hastattr function in python returns only a Boolean value that means True or False.

Python hasattr() function

Python hasattr function is a built-in function that is used to check a specified Object has a specified attribute or not. hasattr function return True or False Boolean value.


The syntax of hasattr function in python is:-

hasattr(Object, attribute)


Python hasattr function takes two parameters:-

  • Object:– Required, An Object.
  • attribute:- The name of the attribute you want to check if exists.

Return Value

Return value of hasattr function is True or False.

Python hasattr example

Here we will take some example to understand hasattr function.


class Student:
	first_name = 'Vishvajit'
	last_name = 'Rao'
	roll_no = 120
	course = 'BCA'
	def Details(self):
		print(f"My name is { self.first_name + self.last_name } and my roll no is {self.roll_no}. I am pursuing {self.course}.")
print(hasattr(Student, 'first_name'))
print(hasattr(Student, 'last_name'))
print(hasattr(Student, 'Details'))
print(hasattr(Student, 'Country'))




In this tutorial, you have learned all about Python hasattr function to check a specified object has a specified attribute or not. If the specified object has a specified attribute, Then it will return True otherwise it will return False.

If you like this article, please keep visiting for further python built-in functions tutorials.

Other Python Built-in Functions

For More Information:- Click Here

Previous Article
Next Article

Related Posts