Reputation: 1806
I'm trying to find information on different ways to traverse an object tree in python. I don't know much about the language in general yet, so any suggestions/techniques would be welcome.
Thanks so much jml
Upvotes: 1
Views: 517
Reputation: 1222
Some way to display what attributes are inside an object could be achieved via pprint.pprint(vars(...))
like so:
import pprint
class TestClass:
def __init__(self):
self.attr1 = "Hello"
self.attr2 = [1, 2, 3]
self.attr3 = {"keyA": "valueA", "keyB": "valueB"}
pprint.pprint(vars(TestClass()))
Output:
{'attr1': 'Hello',
'attr2': [1, 2, 3],
'attr3': {'keyA': 'valueA', 'keyB': 'valueB'}}
Upvotes: 0
Reputation: 41306
See the inspect
module. It has functions for accessing/listing all kinds of object information.
Upvotes: 2