Beispiel #1
0
    def __init__(self, *args, **kwargs):
        # So instead we explicitly call __init__ for all ancestors.
        # The use of *args and **kwargs allows for a clean way to pass arguments,
        # with each parent "peeling a layer of the onion".
        Superhero.__init__(self, 'anonymous', movie=True, 
                           superpowers=['Wealthy'], *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)

        # override the value for the name attribute
        self.name = 'Sad Affleck'
Beispiel #2
0
 def __init__(self, *args, **kwargs):
     # Cách điển hình để thừa kế thuộc tính là gọi super
     # super(Batman, self).__init__(*args, **kwargs)
     # Tuy nhiên với đa thừa kế, super() sẽ chỉ gọi lớp cơ sở tiếp theo trong danh sách MRO.
     # Vì thế, ta sẽ gọi cụ thể hàm __init__ của các lớp chả.
     # Sử dụng *args và **kwargs cho phép việc truyền đối số gọn gàng hơn,
     # trong đó mỗi lớp cha sẽ chịu trách nhiệm cho những phần thuộc về nó
     Human.__init__(self, 'anonymous', *args, **kwargs)
     Bat.__init__(self, *args, can_fly=False, **kwargs)
     # ghi đè giá trị của thuộc tính name
     self.name = 'Sad Affleck'
Beispiel #3
0
 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.
     # So instead we explicitly call __init__ for all ancestors.
     # The use of *args and **kwargs allows for a clean way to pass arguments,
     # with each parent "peeling a layer of the onion".
     Human.__init__(self, 'anonymous', *args, **kwargs)
     Bat.__init__(self, *args, can_fly=False, **kwargs)
     # override the value for the name attribute
     self.name = 'Sad Affleck'
 def __init__(self, *args, **kwargs):
     # Обычно для наследования атрибутов необходимо вызывать super:
     # super(Batman, self).__init__(*args, **kwargs)
     # Однако здесь мы имеем дело с множественным наследованием, а super()
     # работает только со следующим базовым классом в списке MRO.
     # Поэтому вместо этого мы вызываем __init__ для всех родителей.
     # Использование *args и **kwargs обеспечивает чистый способ передачи
     # аргументов, когда каждый родитель "очищает слой луковицы".
     Superhero.__init__(self, 'анонимный', movie=True,
                        superpowers=['Богатый'], *args, **kwargs)
     Bat.__init__(self, *args, can_fly=False, **kwargs)
     # переопределить значение атрибута name
     self.name = 'Грустный Бен Аффлек'
Beispiel #5
0
 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.
     # So instead we explicitly call __init__ for all ancestors.
     # The use of *args and **kwargs allows for a clean way to pass arguments,
     # with each parent "peeling a layer of the onion".
     Superhero.__init__(self, 'anonymous', movie=True, 
                        superpowers=['Wealthy'], *args, **kwargs)
     Bat.__init__(self, *args, can_fly=False, **kwargs)
     # override the value for the name attribute
     self.name = 'Sad Affleck'
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.
        # So instead we explicitly call __init__ for all ancestors.
        # The use of *args and **kwargs allows for a clean way to pass arguments,
        # with each parent "peeling a layer of the onion".
        Superhero.__init__(self, 'anonymous', movie=True, 
                           superpowers=['Wealthy'], *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)
        # override the value for the name attribute
        self.name = 'Sad Affleck'

    def sing(self):
        return 'nan nan nan nan nan batman!'


if __name__ == '__main__':
    sup = Batman()

    # Get the Method Resolution search Order used by both getattr() and super().
    # This attribute is dynamic and can be updated
    print(Batman.__mro__)       # => (<class '__main__.Batman'>, 
                                # => <class 'superhero.Superhero'>, 
                                # => <class 'human.Human'>, 
    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)

        self.name = 'Sad Affleck'