Exemple #1
0
    def __init__(self, can_fly=True):
        self.fly = can_fly

    # This class also has a say method
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # And its own method as well
    def sonar(self):
        return '))) ... ((('


if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))
    print(b.fly)

# And yet another class definition that inherits from Superhero and Bat
# superhero.py
from superhero import Superhero
from bat import Bat


# Define Batman as a child that inherits from both Superhero and Bat
class Batman(Superhero, Bat):
    def __init__(self, *args, **kwargs):
        # Typically to inherit attributes you have to call super:
        # super(Batman, self).__init__(*args, **kwargs)
        # However we are dealing with multiple inheritance here, and super()
        # only works with the next base class in the MRO list.
    def __init__(self, can_fly=True):
        self.fly = can_fly

    # В этом классе также есть метод say
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # И свой метод тоже
    def sonar(self):
        return '))) ... ((('

if __name__ == '__main__':
    b = Bat()
    print(b.say('привет'))
    print(b.fly)


# И еще одно определение класса, унаследованное от Superhero и Bat
# superhero.py
from superhero import Superhero
from bat import Bat

# Определите Batman как дочерний класс, унаследованный от Superhero и Bat
class Batman(Superhero, Bat):

    def __init__(self, *args, **kwargs):
        # Обычно для наследования атрибутов необходимо вызывать super:
        # super(Batman, self).__init__(*args, **kwargs)
        # Однако здесь мы имеем дело с множественным наследованием, а super()
    def __init__(self, can_fly=True):
        self.fly = can_fly

    # This class also has a say method
    def say(self, msg):
        msg = "... ... ..."
        return msg

    # And its own method as well
    def sonar(self):
        return "))) ... ((("


if __name__ == "__main__":
    b = Bat()
    print(b.say("hello"))
    print(b.fly)

# And yet another class definition that inherits from Superhero and Bat
# superhero.py
from superhero import Superhero
from bat import Bat


# Define Batman as a child that inherits from both Superhero and Bat
class Batman(Superhero, Bat):
    def __init__(self, *args, **kwargs):
        # Typically to inherit attributes you have to call super:
        # super(Batman, self).__init__(*args, **kwargs)
        # However we are dealing with multiple inheritance here, and super()
        # only works with the next base class in the MRO list.
Exemple #4
0
    def __init__(self, can_fly=True):
        self.fly = can_fly

    # This class also has a say method
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # And its own method as well
    def sonar(self):
        return '))) ... ((('

if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))
    print(b.fly)


# And yet another class definition that inherits from Superhero and Bat
# superhero.py
from superhero import Superhero
from bat import Bat

# Define Batman as a child that inherits from both Superhero and Bat
class Batman(Superhero, Bat):

    def __init__(self, *args, **kwargs):
        # Typically to inherit attributes you have to call super:
        #super(Batman, self).__init__(*args, **kwargs)      
        # However we are dealing with multiple inheritance here, and super()
    def __init__(self, can_fly=True):
        self.fly = can_fly

    # instance methods - always receive the current class as the 1st arg
    def say(self, msg):
        msg = '... ... ...'
        return msg

    def sonar(self):
        return '))) ... ((('


if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))  # '... ... ...' as msg is overidden in say()
    print(b.fly)  # 'True'

from human import Human
from bat import Bat


class Batman(Human, Bat):

    species = 'Superhero'

    def __init__(self, *args, **kwargs):
        # super(Batman, self).__init__(*args, **kwargs)  # doesn't work for multiple inheritance
        Human.__init__(self, 'anonymous', *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)