Search code examples
pythonclasshasattr

How to test if python class parent has method defined?


I have a subclass that may have a method 'method_x' defined. I want to know if 'method_x' was defined somewhere else up the class hierarchy.

If I do:

hasattr(self, 'method_x')

I'll get a truth value that also looks at any methods defined for the subclass. How do I limit this to just inquire as to whether or not that method was defined higher up the class chain?


Solution

  • If you're using Python 3, you can supply super() to the object parameter of hasattr.

    For example:

    class TestBase:
        def __init__(self):
            self.foo = 1
    
        def foo_printer(self):
            print(self.foo)
    
    
    class TestChild(TestBase):
        def __init__(self):
            super().__init__()
            print(hasattr(super(), 'foo_printer'))
    
    test = TestChild()
    

    With Python 2, it's similar, you just have to be more explicit in your super() call.

    class TestBase(object):
        def __init__(self):
            self.foo = 1
    
        def foo_printer(self):
            print(self.foo)
    
    
    class TestChild(TestBase):
        def __init__(self):
            super(TestChild, self).__init__()
            print(hasattr(super(TestChild, self), 'foo_printer'))
    
    
    test = TestChild()
    

    Both 2 and 3 will work with multiple levels of inheritance and mixins.