1

I'm working with an older version of python, 2.6.5. I can use dir to see what members an object has but I'd like to differentiate among the members in an object versus the members inherited from a parent class.

class Parent(object):

    def parent_method1(self):  
        return 1

    def parent_method2(self):
        return 2

class Child(Parent):

    def child_method1(self):
        return 1

Is there a way to inspect (i.e. dir) an instance of Child object and distinguish which methods are from the Child class and which are inherited from the Parent class?

1
  • 1
    The precise version of Python doesn't matter in this specific case. Commented Aug 10, 2016 at 14:34

1 Answer 1

2

No, dir() does not give that distinction.

You'd have to manually traverse the class MRO and produce the list yourself:

def dir_with_context(cls):
    for c in cls.__mro__:
        for name in sorted(c.__dict__):
            yield (c, name)

This produces:

>>> list(dir_with_context(Child))
[(<class '__main__.Child'>, '__doc__'), (<class '__main__.Child'>, '__module__'), (<class '__main__.Child'>, 'child_method1'), (<class '__main__.Parent'>, '__dict__'), (<class '__main__.Parent'>, '__doc__'), (<class '__main__.Parent'>, '__module__'), (<class '__main__.Parent'>, '__weakref__'), (<class '__main__.Parent'>, 'parent_method1'), (<class '__main__.Parent'>, 'parent_method2'), (<type 'object'>, '__class__'), (<type 'object'>, '__delattr__'), (<type 'object'>, '__doc__'), (<type 'object'>, '__format__'), (<type 'object'>, '__getattribute__'), (<type 'object'>, '__hash__'), (<type 'object'>, '__init__'), (<type 'object'>, '__new__'), (<type 'object'>, '__reduce__'), (<type 'object'>, '__reduce_ex__'), (<type 'object'>, '__repr__'), (<type 'object'>, '__setattr__'), (<type 'object'>, '__sizeof__'), (<type 'object'>, '__str__'), (<type 'object'>, '__subclasshook__')]

The function can easily be augmented to skip names already seen in a subclass:

def dir_with_context(cls):
    seen = set()
    for c in cls.__mro__:
        for name in sorted(c.__dict__):
            if name not in seen:
                yield (c, name)
                seen.add(name)

at which point it produces the exact same number of entries as dir(Child), except for the order the names appear in (the above groups them per class):

>>> sorted(name for c, name in dir_with_context(Child)) == dir(Child)
True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.