[python] Get a list of instance variables

First

Suppose you have the following object:

class Tarou:
    def __init__(self):
        self.name = 'tarou'
        self.age = 15
        
tarou = Tarou()
tarou.height = 170
tarou.weight = 58

Get list

Part ① vars

I remembered the comment from @shiracamus. There was a function that returned in dict format.

print(vars(tarou))

Execution result


{'name': 'tarou', 'age': 15, 'height': 170, 'weight': 58}

Part 2 dict

print(tarou.__dict__)

Execution result


{'name': 'tarou', 'age': 15, 'height': 170, 'weight': 58}

important point

** If it is not the dict attribute, an error will occur. ** **

TypeError: vars() argument must have __dict__ attribute

Get key and value

#Key
tarou.__dict__.keys()

#value
tarou.__dict__.values()

Recommended Posts

[python] Get a list of instance variables
[Python] Get a list of folders only
Python: Get a list of methods for an object
Get the number of specific elements in a python list
Get a list of purchased DMM eBooks with Python + Selenium
Since Python 1.5 of Discord, I can't get a list of members
How to get a list of built-in exceptions in python
Display a list of alphabets in Python 3
Try to get a list of breaking news threads in Python.
Get a list of files in a folder with python without a path
List of python modules
[Python] Get environment variables
[python] Create a list of various character types
Get the caller of a function in Python
Make a copy of the list in Python
Get a list of IAM users with Boto3
Get a list of Qiita likes by scraping
Get a glimpse of machine learning in Python
Get a list of packages installed in your current environment with python
Get a datetime instance at any time of the day in Python
Python: Class and instance variables
Summary of Python3 list operations
Globalization of class instance variables
[python] Get the list of classes defined in the module
Get a list of articles posted by users with Python 3 Qiita API v2
Python list is not a list
Python class variables and instance variables
[Python] Get the list of ExifTags names of Pillow library
Python script to get a list of input examples for the AtCoder contest
[Python] Copy of multidimensional list
Group by consecutive elements of a list in Python
How to get a list of files in the same directory with python
Get the value of a specific key in a list from the dictionary type in the list with Python
Try to get the function list of Python> os package
[Python] How to make a list of character strings character by character
How to shuffle a part of a Python list (at random.shuffle)
[Command] Command to get a list of files containing double-byte characters
Reference order of class variables and instance variables in "self. Class variables" in Python
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
Python: Create a dictionary from a list of keys and values
How to get a list of links from a page from wikipedia
Get index of nth largest / smallest value in list in Python
A record of patching a python package
A good description of Python decorators
[python] Manage functions in a list
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
python / Make a dict from a list.
[Python] Inherit a class with class variables
Generate a list of consecutive characters
About the basics list of Python basics
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Get the value of a specific key up to the specified index in the dictionary list in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Create an instance of a predefined class from a string in Python
[Python] Variables
I tried to create a list of prime numbers with python
[Python] Get the update date of a news article from HTML
[python] Get the rank of the values in List in ascending / descending order