Python hasattr() Method with Examples

In the previous article, we have discussed Python globals() Function with Examples
hasattr() Method in Python:

If the specified object has the specified attribute, the hasattr() function returns true; otherwise, it returns False.

Syntax:

hasattr(object, attribute)

Parameter Values

object: This is required. It is an object.

attribute: The name of the attribute to be checked whether exists or not.

Return Value:

The method hasattr() returns:

  • True if the object has the specified named attribute.
  • False if the object lacks the specified named attribute.

hasattr() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Create a class say Employdetails.
  • Take a variable and initialize it with some random number(id).
  • Take another variable and initialize it with some random name(ename).
  • Create an object for the class and store it in a variable.
  • Check if the above class has an attribute id by passing arguments like the above object and attribute name using the hasattr() function and print the result.
  • Similarly, do the same for the other attribute(jobrole) and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails:
    # Take a variable and initialize it with some random number(id).
    id = 10
    # Take another variable and initialize it with some random name(ename).
    ename = 'Hitler'


# Create an object for the class and store it in a variable.
Employdetailsobj = Employdetails()
# Check if the above class has an attribute id by passing arguments like the
# above object and attribute name using the hasattr() function and print the result.
print('Does Employdetails has id?:', hasattr(Employdetailsobj, 'id'))
# Similarly, do the same for the other attribute(jobrole) and print the result.
print('Does Employdetails has jobrole?:', hasattr(Employdetailsobj, 'jobrole'))

Output:

Does Employdetails has id?: True
Does Employdetails has jobrole?: False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Create a class say Employdetails.
  • Take a variable and initialize it with some random number(id).
  • Take another variable and initialize it with some random name(ename).
  • Create an object for the class and store it in a variable.
  • Give some random attribute name as user input using the input() function and store it in a variable.
  • Give the other attribute name as user input using the input() function and store it in another variable.
  • Check if the above class has the above attribute by passing arguments like the above object and above attribute name using the hasattr() function and print the result.
  • Similarly, do the same for the other attribute and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails:
    # Take a variable and initialize it with some random number(id).
    id = 10
    # Take another variable and initialize it with some random name(ename).
    ename = 'Hitler'


# Create an object for the class and store it in a variable.
Employdetailsobj = Employdetails()
# Give some random attribute name as user input using the input() function
# and store it in a variable.
atrname1 = input("Enter some random attribute name = ")
# Give the other attribute name as user input using the input() function
# and store it in another variable.
atrname2 = input("Enter some random attribute name = ")
# Check if the above class has the above attribute by passing arguments like the above object
# and above attribute name using the hasattr() function and print the result.
print('Does Employdetails has', atrname1, '?:',
      hasattr(Employdetailsobj, atrname1))
# Similarly, do the same for the other attribute and print the result.
print('Does Employdetails has', atrname2, '?:',
      hasattr(Employdetailsobj, atrname2))

Output:

Enter some random attribute name = age
Enter some random attribute name = ename
Does Employdetails has age ?: False
Does Employdetails has ename ?: True

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.