Answers for "python iterate object fields"

2

python iterate over object fields

>>> dir(obj)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bar', 'foo', 'func']

>>> [a for a in dir(obj) if not a.startswith('__')]
['bar', 'foo', 'func']
Posted by: Guest on April-22-2020
4

loop through python object

for attr, value in k.__dict__.items():
        print(attr, value)
Posted by: Guest on February-29-2020
0

iterate through attributes of class python

L = [[getattr(self, attr), attr] for attr in dir(self) if not attr.startswith("__")]
Posted by: Guest on November-23-2020

Python Answers by Framework

Browse Popular Code Answers by Language