jml
jml

Reputation: 1806

traversing an object tree

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

Answers (3)

holzkohlengrill
holzkohlengrill

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

jml
jml

Reputation: 1806

i found out how to do it. basically myobject.membername1.membername2

Upvotes: 0

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41306

See the inspect module. It has functions for accessing/listing all kinds of object information.

Upvotes: 2

Related Questions