Reputation: 9037
I am trying to understand how to use mixins in python. I want to make Bird, Dog and Bat class to have the eat
method from PetMixIn
class but also can be self instance. How should I do it?
class Pet(object):
def __init__(self, food):
self.food = food
def eat(self):
print(f'Eatting...{self.food}')
class PetMixIn(object):
def eat(self):
print('Eatting...')
class Animal(object):
def __init__(self, life):
self.liferange = life
class Bird(Animal):
def __init__(self, life, flyable):
super.__init__(lift)
self.flyable = flyable
#bird attribution ...
class Dog(Animal):
def __init__(self, life, name):
super.__init__(lift)
self.name = name
#dog attribution ...
class Bat(Animal):
def __init__(self, life, size):
super.__init__(lift)
self.size = size
#bat attribution ...
bat = Bat(40, '1')
dog = Dog(10, 'tom')
bird = Bird(3, True)
Upvotes: 1
Views: 232
Reputation: 1788
Here a small example (python3)
class PetMixIn:
xxx = 2
def eat(self):
print('Eatting...')
class Animal:
def __init__(self, life):
self.liferange = life
class Bat(Animal, PetMixIn):
def __init__(self, life, size):
super().__init__(life)
self.size = size
#bat attribution ...
bat = Bat(40, '1')
bat.eat()
print(bat.xxx)
Upvotes: 2