Jump to content
  • Advertisement
Sign in to follow this  
samoz

Python Inheritance

This topic is 3657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to learn Python so that I can implement an inheritance tree. Can someone please explain or point me to some resources on the super() operator? I read the Python documentation and googled, but I found nothing that made sense to me... Let's say I have:
class Test1:
   def __init__(self):
      print 'Inside Test1 __init__'

   def func1(self):
      return 5;
Is this the proper way to inherit from it?
class Test2(Test):
   def __init__(self):
      super(Test2, self).__init__()
      print 'Inside Test2 __init__'
  
   def func2(self):
      return super(Test2, self).func1() + 5
And then is there anything special to do to inherit from Test2? And how would I reference a function of Test1 from an object of Test3? (Assuming that Test3 assumes from Test2) Would it be nested super calls?
n = Test3()
super(Test2, super(Test3,n)).func1()

Share this post


Link to post
Share on other sites
Advertisement
I think what you are trying to do is something like this:



class Test1:
def __init__(self):
print "Inside Test1 __init__"

def func(self):
return 5

class Test2(Test1):
def __init__(self):
Test1.__init__(self)
print "Inside Test2 __init__"

def func(self):
return Test1.func(self)+5

class Test3(Test2):
def __init__(self):
Test2.__init__(self)
print "Inside Test3 __init__"

foo = Test3()
print foo.func()





This will print


Inside Test1 __init__
Inside Test2 __init__
Inside Test3 __init__
10



Edit: To reference a Test1 func from Test3, do this:


print Test1.func(foo)


That will give you the

5

Share this post


Link to post
Share on other sites
So if you can access superclass functions directly like that, what is the point of the super() function?

I have been reading today and found that my top class(Test1) must extend the object class for my super() calls to function properly.

I have pulled at least a pound of hair out today working on this but I think I'm starting to get it.

Share this post


Link to post
Share on other sites
I'm by no means an expert on python, but I've never used the super function in my code. I know that the syntax I used works, and does what one would expect.

There is no direct super class in python, because all objects have the potential to implement multiple-inheritance.

Share this post


Link to post
Share on other sites
In situations of single inheritence(when your class has only one ancestor), super() and direct call to the base class do pretty much the same.
However, super() is used to resolve the diamond problems in situation of multiple inheritance. Imagine when a class inherits from 2 classes that have a common ancestor:


class Common(object):
def __init__(self):
print "Common.init"

class A(Common):
def __init__(self):
Common.__init__(self)
print "A.init"

class B(Common):
def __init__(self):
Common.__init__(self)
print "B.init"

class Foo(A,B):
def __init__(self):
A.__init__(self)
B.__init__(self)




If you follow the code flow carefully, you will see that Common.__init__() is called 2 times when we create a Foo object. super() resolves that, because it calls the next method in MRO(Method Resolution Order). So if we have this:


class Common(object):
def __init__(self):
print "Common.init"

class A(Common):
def __init__(self):
super(A,self).__init__()
print "A.init"

class B(Common):
def __init__(self):
super(B,self).__init__()
print "B.init"

class Foo(A,B):
def __init__(self):
super(Foo,self).__init__()



Now: The MRO for a Foo object is (Foo,A,B,Common). super(class,self) returns the next class in the MRO for the object 'self'. So using super each constructor is called exactly one time in the correct order.

I generally don't use super(), because I don't employ multiple inheritance. I consider that there are better alternatives. If you use single inheritance only, there's really no need to use super(), although it wouldn't really hurt either.

Share this post


Link to post
Share on other sites
super() isn't necessarily useful. The usual way to do these things is by calling the superclass method directly, as shown. However, you should probably be inheriting your base class from object *anyway*. It lets you use other nice stuff as well, such as properties, and doesn't really cost you anything (unless you are very accustomed to certain old-fashioned things from previous Python versions).

You don't need to nest super() calls like that, unless you need to figure out what the parent class is dynamically. super(classname, instance) looks for the class which is next in the MRO, after classname of the instance. So you can let it be a superclass of the instance, and (if you have no multiple inheritance!) you'll get the direct superclass of that superclass. On the other hand, if you do know what the parent is, then you probably know what the parent of the parent is, and should probably just use it directly. And on the other other hand, if you need to do really complicated lookup, you (a) almost certainly have a design problem and (b) might want to look things up manually in the __mro__ instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
However, you should probably be inheriting your base class from object *anyway*.

Especially since super() is only available for new-style classes (ie, classes derived from object).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!