Python getattr() Function – Tutorial with Examples

The getattr() function in Python is a built-in function that is used to get the value of an object’s attribute. The function takes two arguments: an object and a string representing the name of the attribute to be retrieved. If the attribute exists, the function returns its value, otherwise it returns an AttributeError. The getattr() function is useful in cases where you want to access an attribute of an object, but you’re not sure if the attribute exists or not. You can also specify a default value to be returned if the attribute does not exist.

Syntax

getattr(object, attribute_name, default_value)

Parameters

  • object – The object whose attribute is to be retrieved.
  • attribute_name – The name of the attribute to be retrieved as a string.
  • default_value – The default value to be returned if the attribute does not exist. This is an optional parameter.

Return Value

The getattr() function returns the value of the specified attribute of the given object. If the attribute does not exist, and no default value is specified, an AttributeError is raised.

Examples

Example 1: Retrieving an Attribute’s Value

class Person:
    name = "John Doe"
    age = 30
person = Person()
print(getattr(person, "name"))

Output:

John Doe

In this example, a class named Person is defined with two attributes: name and age. An object of the class is then created and stored in the person variable. The getattr() function is then used to retrieve the value of the name attribute of the person object. The function returns the value of the attribute, which is "John Doe".

Example 2: Retrieving an Attribute with a Default Value

class Person:
    name = "John Doe"
    age = 30
person = Person()
print(getattr(person, "address", "Unknown"))

Output:

Unknown

In this example, a class named Person is defined with two attributes: name and age. An object of the class is then created and stored in the person variable. The getattr() function is then used to retrieve the value of an attribute named address of the person object. Since the attribute does not exist, the function returns the default value specified, which is "Unknown".

Example 3: Handling AttributeError with getattr()

class Person:
    name = "John Doe"
    age = 30
person = Person()
try:
    print(getattr(person, "address"))
except AttributeError:
    print("Attribute not found")

Output:

Attribute not found

In this example, a class named Person is defined with two attributes: name and age. An object of the class is then created and stored in the person variable. The getattr() function is then used to retrieve the value of an attribute named address of the person object. Since the attribute does not exist, an AttributeError is raised. The error is handled using a tryexcept block and a custom message is printed instead of the error message.

Use Cases

The getattr() function is useful in a number of scenarios, including:

  • Retrieving an attribute of an object when the name of the attribute is stored in a variable.
  • Dealing with optional attributes that may or may not exist in an object.
  • Providing default values for attributes that do not exist in an object.
  • Handling AttributeError exceptions gracefully by providing custom error messages or alternative values.

The getattr() function is a powerful and versatile tool that can be used to safely access the attributes of an object, making your code more robust and flexible.

Leave a Reply

Your email address will not be published. Required fields are marked *