learn-python

Форк
0
/
test_multiple_inheritance.py 
68 строк · 2.8 Кб
1
"""Multiple Inheritance
2

3
@see: https://docs.python.org/3/tutorial/classes.html#multiple-inheritance
4

5
Some classes may derive from multiple classes. This means that the derived class would have
6
its attributes, along with the attributes of all the classes that it was derived from.
7
"""
8

9

10
def test_multiple_inheritance():
11
    """Multiple Inheritance"""
12

13
    # pylint: disable=too-few-public-methods
14
    class Clock:
15
        """Clock class"""
16

17
        time = '11:23 PM'
18

19
        def get_time(self):
20
            """Get current time
21

22
            Method is hardcoded just for multiple inheritance illustration.
23
            """
24
            return self.time
25

26
    # pylint: disable=too-few-public-methods
27
    class Calendar:
28
        """Calendar class"""
29

30
        date = '12/08/2018'
31

32
        def get_date(self):
33
            """Get current date
34

35
            Method is hardcoded just for multiple inheritance illustration.
36
            """
37
            return self.date
38

39
    # Python supports a form of multiple inheritance as well. A class definition with multiple
40
    # base classes looks like this.
41
    class CalendarClock(Clock, Calendar):
42
        """Class that uses multiple inheritance.
43

44
        For most purposes, in the simplest cases, you can think of the search for attributes
45
        inherited from a parent class as depth-first, left-to-right, not searching twice in the same
46
        class where there is an overlap in the hierarchy. Thus, if an attribute is not found in
47
        CalendarClock, it is searched for in Clock, then (recursively) in the base classes of
48
        Clock, and if it was not found there, it was searched for in Calendar, and so on.
49

50
        In fact, it is slightly more complex than that; the method resolution order changes
51
        dynamically to support cooperative calls to super(). This approach is known in some other
52
        multiple-inheritance languages as call-next-method and is more powerful than the super call
53
        found in single-inheritance languages.
54

55
        Dynamic ordering is necessary because all cases of multiple inheritance exhibit one or more
56
        diamond relationships (where at least one of the parent classes can be accessed through
57
        multiple paths from the bottommost class). For example, all classes inherit from object,
58
        so any case of multiple inheritance provides more than one path to reach object. To keep
59
        the base classes from being accessed more than once, the dynamic algorithm linearizes the
60
        search order in a way that preserves the left-to-right ordering specified in each class,
61
        that calls each parent only once, and that is monotonic (meaning that a class can be
62
        subclassed without affecting the precedence order of its parents).
63
        """
64

65
    calendar_clock = CalendarClock()
66

67
    assert calendar_clock.get_date() == '12/08/2018'
68
    assert calendar_clock.get_time() == '11:23 PM'
69

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.