Python | getattr() method

getattr() function is used to access the attribute value of an object and also give an option of executing the default value in case of unavailability of the key. This has greater application to check for available keys in web development and many other phases of day-to-day programming.

Syntax : getattr(obj, key, def)

Parameters :
obj : The object whose attributes need to be processed.
key : The attribute of object
def : The default value that need to be printed in case attribute is not found.

Returns :
Object value if value is available, default value in case attribute is not present
and returns AttributeError in case attribute is not present and default value is not
specified.

Code #1 : Demonstrating working of getattr()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# working of getattr()
  
# declaring class 
class GfG :
    name = "GeeksforGeeks"
    age = 24
  
# initializing object
obj = GfG()
  
# use of getattr
print("The name is " + getattr(obj,'name'))
  
# use of getattr with default
print("Description is " + getattr(obj, 'description' , 'CS Portal'))
  
# use of getattr without default
print("Motto is " + getattr(obj, 'motto'))

chevron_right


Output:

The name is GeeksforGeeks
Description is CS Portal

Exception:



AttributeError: GfG instance has no attribute 'motto'

 

Code #2 : Performance Analysis

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# performance analysis of getattr()
import time 
  
# declaring class 
class GfG :
    name = "GeeksforGeeks"
    age = 24
  
# initializing object
obj = GfG()
  
# use of getattr to print name
start_getattr = time.time()
print("The name is " + getattr(obj,'name'))
print("Time to execute getattr " + str(time.time() - start_getattr))
  
# use of conventional method to print name
start_obj = time.time()
print("The name is " + obj.name)
print("Time to execute conventional method " + str(time.time() - start_obj))

chevron_right


Output:

The name is GeeksforGeeks
Time to execute getattr 5.0067901611328125e-06
The name is GeeksforGeeks
Time to execute conventional method 1.1920928955078125e-06

Result : Conventional method takes less time than getattr(), but when default values have to be used in case of missing attributes, getattr() is a good choice.

Applications : The are many applications of getattr(), few of them already mentioned in cases of absence of attributes of objects, in web developments where some of form attributes are optional. Also useful in cases of Machine Learning feature collections in case some features sometimes go missing in data collection.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.