Multiple inheritance, Mixins, locals and globals

Recap

  • What is inheritance?
  • What does super() do?

Multiple inheritance

class Soul:
    def __init__(self):
        print('so spiritual...')


class Body:
    def __init__(self):
        print('much physics...')



class Human(Soul, Body):
    def __init__(self):
        print('very completed...')



me = Human()
# very completed...

Know the difference

class Soul:
    def __init__(self):
        print('so spiritual...')

class Body:
    def __init__(self):
        print('much physics...')

class Human(Soul, Body):
    def __init__(self):
        super().__init__()

me = Human()
# so spiritual...
class Soul:
    def __init__(self):
        print('so spiritual...')

class Body:
    def __init__(self):
        print('much physics...')

class Human(Body, Soul):
    def __init__(self):
        super().__init__()

me = Human()
# much physics

#1

#2

Going deeper in inheritance

  •  MRO (Method Resolution Order)
  • Inheritance in Python is from right to left
  • playing with super()
  • Foo.mro()
  • dir(foo)

Mixins conception

  • Another level of abstraction
  • Python's "interfaces"
  • Why should I use it?
    • You want to have a feature that all inheritors have
    • You want to have a feature that some of the inheritors have
  • Mixins are usually described as being included rather than being inherited

locals() and globals()

  • globals() - gives you a dictionary representation of the current module namespace
  • locals() - gives you a dictionary representation of the current namespace

Multiple inheritance, Mixins, locals and globals

By Hack Bulgaria

Multiple inheritance, Mixins, locals and globals

  • 1,098