sup = Superhero(name="Тик")

    # Проверка типа экземпляра
    if isinstance(sup, Human):
        print('Я человек')
    if type(sup) is Superhero:
        print('Я супергерой')

    # Получить порядок поиска разрешения метода (MRO),
    # используемый как getattr(), так и super()
    # Этот атрибут является динамическим и может быть обновлен
    print(Superhero.__mro__)    # => (<class '__main__.Superhero'>,
                                # => <class 'human.Human'>, <class 'object'>)

    # Вызывает родительский метод, но использует свой собственный атрибут класса
    print(sup.get_species())    # => Сверхчеловек

    # Вызов переопределенного метода
    print(sup.sing())           # => Бам, бам, БАМ!

    # Вызывает метод из Human
    sup.say('Ложка')            # => Тик: Ложка

    # Метод вызова, существующий только в Superhero
    sup.boast()                 # => Я обладаю силой 'сверхсила'!
                                # => Я обладаю силой 'пуленепробиваемость'!

    # Атрибут унаследованного класса
    sup.age = 31
    print(sup.age)              # => 31
Beispiel #2
0
if __name__ == '__main__':
    sup = Superhero(name="Tick")

    # Instance type checks
    if isinstance(sup, Human):
        print('I am human')
    if type(sup) is Superhero:
        print('I am a superhero')

    # Get the Method Resolution search Order used by both getattr() and super()
    # This attribute is dynamic and can be updated
    print(Superhero.__mro__)  # => (<class '__main__.Superhero'>,
    # => <class 'human.Human'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())  # => Superhuman

    # Calls overridden method
    print(sup.sing())  # => Dun, dun, DUN!

    # Calls method from Human
    sup.say('Spoon')  # => Tick: Spoon

    # Call method that exists only in Superhero
    sup.boast()  # => I wield the power of super strength!
    # => I wield the power of bulletproofing!

    # Inherited class attribute
    sup.age = 31
    print(sup.age)  # => 31
Beispiel #3
0
if __name__ == '__main__':
    sup = Superhero(name="Tick")

    # Instance type checks
    if isinstance(sup, Human):
        print('I am human')
    if type(sup) is Superhero:
        print('I am a superhero')

    # Get the Method Resolution search Order used by both getattr() and super()
    # This attribute is dynamic and can be updated
    print(Superhero.__mro__)    # => (<class '__main__.Superhero'>,
                                # => <class 'human.Human'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())    # => Superhuman

    # Calls overloaded method
    print(sup.sing())           # => Dun, dun, DUN!

    # Calls method from Human
    sup.say('Spoon')            # => Tick: Spoon

    # Call method that exists only in Superhero
    sup.boast()                 # => I wield the power of super strength!
                                # => I wield the power of bulletproofing!

    # Inherited class attribute
    sup.age = 31
    print(sup.age)              # => 31