Difference between python built-in getattr() and hasattr() function with practical example

Python getattr() Function

The python getattr() function returns the value of a named attribute of an object. If it is not found, it returns the default value.

Python getattr() Function Example

class Details:  
    age = 22  
    name = "Phill"  
  
    details = Details()  
    print('The age is:', getattr(details, "age"))  
    print('The age is:', details.age)  

Output:

The age is: 22
The age is: 22

Python hasattr() Function

The python any() function returns true if any item in an iterable is true, otherwise it returns False.

Python hasattr() Function Example

l = [4, 3, 2, 0]                              
print(any(l))                                   
  
l = [0, False]  
print(any(l))  
  
l = [0, False, 5]  
print(any(l))  
  
l = []  
print(any(l))  

Output:

True
False
True
False