Re: Concept of inheritance and explanations?

Super is a bit of a tricky function to use at times, I'm not entirely sure if there are differences between python 2 and python 3 with it. Let's try a different example here, every child class has an __mro__ attribute before its created, this tells you the order that the classes are called when initializing a class with single, or multiple inheritance. Now if the kid class has an __init__() constructor or a function of the same name as its parents, it will ignore those functions but inherit all the other functions and variables they have. So for example:

class mother(object):
    def __init__(self):
        self.x = 1
    def stuff(self):
        pass

class father(object):
    def __init__(self):
        self.y = 2
    def muff(self):
        pass

class kid(mother,father):
    def __init__(self):
        self.z = 3
    def stuff(self):
        print('stuff')
    def muff(self):
        print('muff')

a = kid()
print(dir(a))

kid in this case would inherit nothing from its parents because it already has an __init__(), stuff(), and muff() function. But you can still call its parent classes functions directly if you want to, such as __init__() like so:

class mother(object):
    def __init__(self):
        self.x = 1
    def stuff(self):
        pass

class father(object):
    def __init__(self):
        self.y = 2
    def muff(self):
        pass

class kid(mother,father):
    def __init__(self):
        self.z = 3
    def stuff(self):
        print('stuff')
        mother.__init__(self)
    def muff(self):
        print('muff')

a = kid()
a.stuff()
print(dir(a))

This gives the kid class its mothers variables from __init__(). Now the way super works involves the Method Resolution Order, or __mro__(). Example:

class mother(object):
    def __init__(self):
        self.x = 1
    def stuff(self):
        pass

class father(object):
    def __init__(self):
        self.y = 2
    def muff(self):
        pass

class kid(mother,father):
    def __init__(self):
        self.z = 3

print(kid.__mro__)
a = kid()
(<class '__main__.kid'>, <class '__main__.mother'>, <class '__main__.father'>, <type 'object'>)

So this shows the order the classes are called in, kid, mother, father, and the type of object. When using super you need the class to call, the type, and the function to call. So super(kid,self).__init__() would call the class thats next in line in the mro list, which would be mother. If you called super(mother,self).__init__() it would call the next in line, which would be father. Not the most super intuitive compared to just calling the parent classes directly, but eh. Like I said, there may be some differences between python 2 and python 3, and nesting the super calls between parent and child classes can be better for chaining inheritance calls along a tree list class structure. There's some more information available [here] and [here].

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector

Reply via email to