Access object attribute by String Name in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Access object attribute by String Name in Python
  2. Specifying a default value for when the attribute doesn't exist
  3. Accessing a method given a string name
  4. Check if an object's attribute exists given a string name

# Access object attribute by String Name in Python

Use the getattr() function to access an object attribute by string.

The getattr() function returns the value of the provided attribute of the object.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('bobbyhadz', 100) print(getattr(emp1, 'name')) # ๐Ÿ‘‰๏ธ bobbyhadz print(getattr(emp1, 'salary')) # ๐Ÿ‘‰๏ธ 100

access object attribute by string name

The code for this article is available on GitHub

We used the getattr() function to access an object's attribute given a string name.

If you need to access the object's attribute inside of a method, you would pass self as the first argument to getattr().

The getattr() function can also be used to access an object's method using a string name.

The getattr() function returns the value of the provided attribute of the object.

The function takes the following parameters:

NameDescription
objectthe object whose attribute should be retrieved
namethe name of the attribute
defaulta default value for when the attribute doesn't exist on the object

# Specifying a default value for when the attribute doesn't exist

You can pass a third argument to the getattr() function if you need to specify a default value for when an attribute doesn't exist.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('bobbyhadz', 100) print(getattr(emp1, 'another', None)) # ๐Ÿ‘‰๏ธ None

specify default value for when attribute does not exist

The code for this article is available on GitHub

An attribute with the supplied name doesn't exist, so the default value of None is returned.

# Accessing a method given a string name

The same syntax can be used to access a method by providing a string name.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('bobbyhadz', 100) method = getattr(emp1, 'get_name') print(method()) # ๐Ÿ‘‰๏ธ bobbyhadz

accessing method given string name

The getattr() function returns the specified method, so we have to use another set of parentheses to call the method.

# Check if an object's attribute exists given a string name

You can use the hasattr() function if you need to check if an object's attribute exists given a string name.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('bobbyhadz', 100) print(hasattr(emp1, 'another')) # ๐Ÿ‘‰๏ธ False if hasattr(emp1, 'salary'): print(getattr(emp1, 'salary')) # ๐Ÿ‘‰๏ธ 100

check if object attribute exists given string name

The code for this article is available on GitHub

The hasattr() function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe name of the attribute to check for in the object
The hasattr() function returns True if the string is the name of one of the object's attributes, otherwise False is returned.

You can use the delattr() function if you need to delete an object's attribute given a string name.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('bobbyhadz', 100) delattr(emp1, 'salary') print(hasattr(emp1, 'salary')) # ๐Ÿ‘‰๏ธ False

The delattr() function deletes the given attribute from the object.

The delattr function takes the following 2 parameters:

NameDescription
objectThe object we want to delete an attribute from
nameThe name of the attribute to delete from the object

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev