Last updated: Apr 10, 2024
Reading timeยท3 min
Use the getattr()
function to access an object attribute by string.
The getattr()
function returns the value of the provided attribute of the
object.
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
We used the getattr()
function to access an object's attribute given a string
name.
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:
Name | Description |
---|---|
object | the object whose attribute should be retrieved |
name | the name of the attribute |
default | a default value for when the attribute doesn't exist on the object |
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.
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
An attribute with the supplied name doesn't exist, so the default value of
None
is returned.
The same syntax can be used to access a method by providing a string name.
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
The getattr()
function returns the specified method, so we have to use another
set of parentheses to call the method.
You can use the hasattr()
function if you need to check if an object's
attribute exists given a string name.
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
The hasattr() function takes the following 2 parameters:
Name | Description |
---|---|
object | The object we want to test for the existence of the attribute |
name | The name of the attribute to check for in the object |
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.
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:
Name | Description |
---|---|
object | The object we want to delete an attribute from |
name | The name of the attribute to delete from the object |
You can learn more about the related topics by checking out the following tutorials: