0
class Test(object):
    def __init__(self, name, myclass):
        self.name = name
        self.myclass = myclass

   def get_parents(self):
        ? self.myclass.super() ?
        return parent

obj = Art # another class
testobj = Test('testA', obj)
print(testobj.get_parents())

The parent class of Test can be obtained using super().

How to get the parent classes of Art inside Test class? (Say Art is inherited from Artists and Artists from Base. We need to be able to get Artists and Base as parents.)

1010101
  • 843
  • 1
  • 6
  • 11
  • 2
    _"The parent class of Test can be obtained using super()"_ That is not correct. `super` doesn't give you the parent class, it gives you a proxy object. – Aran-Fey Jan 31 '18 at 12:27
  • Yes. I meant the parent class can be accessed. – 1010101 Jan 31 '18 at 12:30

1 Answers1

1

You can get the method resolution order with mro():

class Test(object):
    def __init__(self, name, myclass):
        self.name = name
        self.myclass = myclass

    def get_parents(self):
        return self.myclass.mro()

class Base:
    pass

class Artists(Base):
    pass

class Art(Artists):
    pass

testobj = Test('testA', Art)
print(testobj.get_parents())

Output:

[<class '__main__.Art'>, <class '__main__.Artists'>, <class '__main__.Base'>, <class 'object'>]

This should come close to:

We need to be able to get Artists and Base as parents.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161