cython

Форк
0
/
py2_super.pyx 
97 строк · 1.8 Кб
1
# mode: run
2
# tag: py3k_super
3

4
class A(object):
5
    def method(self):
6
        return 1
7

8
    @classmethod
9
    def class_method(cls):
10
        return 2
11

12
    @staticmethod
13
    def static_method():
14
        return 3
15

16
    def generator_test(self):
17
        return [1, 2, 3]
18

19

20
class B(A):
21
    """
22
    >>> obj = B()
23
    >>> obj.method()
24
    1
25
    >>> B.class_method()
26
    2
27
    >>> B.static_method(obj)
28
    3
29
    >>> list(obj.generator_test())
30
    [1, 2, 3]
31
    """
32
    def method(self):
33
        return super(B, self).method()
34

35
    @classmethod
36
    def class_method(cls):
37
        return super(B, cls).class_method()
38

39
    @staticmethod
40
    def static_method(instance):
41
        return super(B, instance).static_method()
42

43
    def generator_test(self):
44
        for i in super(B, self).generator_test():
45
            yield i
46

47

48
cdef class CClassBase(object):
49
    def method(self):
50
        return 'def'
51
    cpdef method_cp(self):
52
        return 'cpdef'
53

54
#     cdef method_c(self):
55
#         return 'cdef'
56
#     def call_method_c(self):
57
#         return self.method_c()
58

59
cdef class CClassSub(CClassBase):
60
    """
61
    >>> CClassSub().method()
62
    'def'
63
    >>> CClassSub().method_cp()
64
    'cpdef'
65
    """
66
#     >>> CClassSub().call_method_c()
67
#     'cdef'
68

69
    def method(self):
70
        return super(CClassSub, self).method()
71
    cpdef method_cp(self):
72
        return super(CClassSub, self).method_cp()
73

74
#     cdef method_c(self):
75
#         return super(CClassSub, self).method_c()
76

77
cdef class Base(object):
78
    """
79
    >>> Base().method()
80
    'Base'
81
    >>> Base.method(Base())
82
    'Base'
83
    """
84
    cpdef method(self):
85
        return "Base"
86

87
cdef class Sub(Base):
88
    """
89
    >>> Sub().method()
90
    'Sub'
91
    >>> Sub.method(Sub())
92
    'Sub'
93
    >>> Base.method(Sub())
94
    'Base'
95
    """
96
    cpdef method(self):
97
        return "Sub"
98

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

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

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

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