In this tutorial we will learn about python getattr() method and its uses.

Python getattr() Method

What is python getattr() method?

Python getattr() method is used to access the values of attributes of python objects. If a specified object has no attributes, it will return the default value provided to the method.

The syntax of python getattr() is:

getattr(object, attribute, default)

Python getattr() method Parameters

The getattr() method takes 3 parameters:

  • object – Name of the object whose attributes value want to be accessed.
  • name – normal string that contains the name of an attribute.
  • default (optional) – Default value which will be returned when given attributes is not found.

getattr() is also equivalent to:

object.name 

Both getattr() and “object.name” will do the same thing.

Let’s check an example of the getattr() method.

Example 1: How to use the getattr() method on the python class?

class Dog:
    name="Rocky"
    age = 6
    color = "White"

dog = Dog()

print("The age is:", getattr(dog,"age"))
print("The color is:", getattr(dog,"color"))
print("The bread is:", getattr(dog,"bread","Husky"))

When we run the above program we will get following output:

The age is: 6
The color is: White
The bread is: Husky

Example 2: Accessing attributes that are not declared.

class Dog:
    name="Rocky"
    age = 6
    color = "White"

dog = Dog()

print("The bread is:", getattr(dog,"bread"))

The output will be as follow:

Traceback (most recent call last):
  File "", line 8, in <module>
    print("The bread is:", getattr(dog,"bread"))
AttributeError: 'Dog' object has no attribute 'bread'

As you can see we are accessing attributes that are not present in the object, and it throws an error as “object has no attribute” if in the above program we use default parameter, it will not raise any error.

 We can do the same thing by calling the class with the attribute name.

Example 3: Accessing object value without getatt() method.

class Dog:
    name="Rocky"
    age = 6
    color = "White"

dog = Dog()

print("The age is:", dog.age)
print("The color is:", dog.color)

below is the output:

The age is: 6
The color is: White

Rules of getattr() method

  • It will only return the value of the attribute if it is found.
  • If the attribute is not present and the default value is not given AttributeError exception will occur.
  • It will return default if no named attribute is found.

 

Was this article helpful?
Share on: