AppDividend
Latest Code Tutorials

Python getattr() Example | getattr() Function Tutorial

0

Python getattr() Example | getattr() Function Tutorial is today’s topic. The getattr() method returns a value of the named attribute of an object. If it is not found, then it returns the default value provided to the function. The getattr() function returns the value of the specified attribute from the specified object.

Python getattr()

The 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.

See the following syntax.

getattr(object, attribute, default)

The object parameter is required, and it is an object.

The attribute is the name of the attribute you want to get the value from.

The default parameter is optional, and it is the value to return if the attribute does not exist.

See the following code example.

# app.py

class StrangerThings:
    name = "Eleven"
    age = 11
    country = "USA"


name = getattr(StrangerThings, 'name')
age = getattr(StrangerThings, 'age')
country = getattr(StrangerThings, 'country')
print(name)
print(age)
print(country)

See the following output.

➜  pyt python3 app.py
Eleven
11
USA
➜  pyt

#getattr() Function With Object

Okay, let’s take an example in which we create an object and then access its properties using getattr() function.

# app.py

class StrangerThings:
    name = "Eleven"
    age = 11
    country = "USA"


duffers = StrangerThings()

print(getattr(duffers, 'name'))
print(getattr(duffers, 'age'))
print(getattr(duffers, 'country'))

So, we have created an object called duffers, and then we used the getattr() function to get all the attribute values.

#Default Value

Okay, let’s add the default value to the getattr() function.

# app.py

class StrangerThings:
    name = "Eleven"
    age = 11
    country = "USA"


duffers = StrangerThings()

print(getattr(duffers, 'catchphrase', 'Friends Don\'t lie'))

In the above code, we have provided the default value to the catchphrase property.

See the following output.

➜  pyt python3 app.py
Friends Don't lie
➜  pyt

Let’s say we do not provide the default value to the new property which does not exist in object and see the output.

# app.py

class StrangerThings:
    name = "Eleven"
    age = 11
    country = "USA"


duffers = StrangerThings()

print(getattr(duffers, 'catchphrase'))

See the following output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 9, in <module>
    print(getattr(duffers, 'catchphrase'))
AttributeError: 'StrangerThings' object has no attribute 'catchphrase'
➜  pyt

That means the StrangerThings object has no attribute catchphrase. So we can not get its value, which does not even exist.

If we don’t provide any default value when the named attribute catchphrase is not found, it raises an AttributeError saying the object has no catchphrase attribute.

So, Use the “default” parameter to write a message when the attribute does not exist.

Finally, Python getattr() Example | getattr() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.