Get all instance variables in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Get all instance variables in Python #

To get all instance variables:

  1. Use the __dict__ attribute to get a dictionary of the object's properties and values.
  2. Use the dict.keys() method to get the names of the instance variables.
  3. Use the list() class to convert the result to a list.
main.py
class Employee(): def __init__(self, id, name, salary): self.id = id self.name = name self.salary = salary bob = Employee(1, 'bobbyhadz', 100) result = list(bob.__dict__.keys()) print(result) # 👉️ ['id', 'name', 'salary'] print(bob.__dict__) # 👉️ {'id': 1, 'name': 'bobbyhadz', 'salary': 100}

We used the __dict__ attribute to get all instance variables of an object.

The __dict__ attribute returns a dictionary containing the object's properties and values.

main.py
class Employee(): def __init__(self, id, name, salary): self.id = id self.name = name self.salary = salary bob = Employee(1, 'bobbyhadz', 100) # 👇️ {'id': 1, 'name': 'bobbyhadz', 'salary': 100} print(bob.__dict__)

You can use the dict.keys() method to get only the keys of the dictionary.

main.py
class Employee(): def __init__(self, id, name, salary): self.id = id self.name = name self.salary = salary bob = Employee(1, 'bobbyhadz', 100) result = list(bob.__dict__.keys()) print(result) # 👉️ ['id', 'name', 'salary']

The dict.keys method returns a new view of the dictionary's keys.

The last step is to use the list() class to convert the view to a list.

Even if the class had class variables, they wouldn't be contained in the dictionary the __dict__ attribute returns.

main.py
class Employee(): cls_id = 'employee' def __init__(self, id, name, salary): self.id = id self.name = name self.salary = salary bob = Employee(1, 'bobbyhadz', 100) result = list(bob.__dict__.keys()) print(result) # ['id', 'name', 'salary'] print(bob.__dict__) # 👉️ {'id': 1, 'name': 'bobbyhadz', 'salary': 100}

Alternatively, you can use the vars() function.

Get all instance variables using vars() #

To get all instance variables:

  1. Use the vars() function to get a dictionary of the object's properties and values.
  2. Use the dict.keys() method to get the names of the instance variables.
  3. Use the list() class to convert the result to a list.
main.py
class Employee(): cls_id = 'employee' def __init__(self, id, name, salary): self.id = id self.name = name self.salary = salary bob = Employee(1, 'bobbyhadz', 100) result = list(vars(bob).keys()) print(result) # 👉️ ['id', 'name', 'salary'] # 👇️ {'id': 1, 'name': 'bobbyhadz', 'salary': 100} print(vars(bob))

The vars function takes an object and returns the __dict__ attribute of the given module, class, instance or any other object that has a __dict__ attribute.

The vars() function raises a TypeError if the provided object doesn't have a __dict__ attribute.

Which approach you pick is a matter of personal preference. I'd use the __dict__ attribute directly to be more explicit.

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.