cython

Форк
0
/
staticmethod.pyx 
140 строк · 2.6 Кб
1
cimport cython
2

3

4
class class1:
5
    u"""
6
    >>> class1.plus1(1)
7
    2
8
    >>> class1().plus1(1)
9
    2
10
    >>> class1.bplus1(1)
11
    2
12
    >>> class1().bplus1(1)
13
    2
14
    """
15
    @staticmethod
16
    def plus1(a):
17
        return a + 1
18

19
    @staticmethod
20
    @cython.binding(True)
21
    def bplus1(a):
22
        return a + 1
23

24

25
def nested_class():
26
    """
27
    >>> cls = nested_class()
28
    >>> cls.plus1(1)
29
    2
30
    >>> obj = cls()
31
    >>> obj.plus1(1)
32
    2
33
    """
34
    class class2(object):
35
        def __new__(cls): # implicit staticmethod
36
            return object.__new__(cls)
37

38
        @staticmethod
39
        def plus1(a):
40
            return a + 1
41
    return class2
42

43

44
cdef class BaseClass(object):
45
    """
46
    Test cdef static methods with super() and Python subclasses
47

48
    >>> obj = BaseClass()
49
    >>> obj.mystaticmethod(obj, 1)
50
    1
51
    >>> BaseClass.mystaticmethod(obj, 1)
52
    1
53
    >>> obj.mystaticmethod2(1, 2, 3)
54
    1 2 3
55
    >>> BaseClass.mystaticmethod2(1, 2, 3)
56
    1 2 3
57
    """
58

59
    @staticmethod
60
    def mystaticmethod(self, arg1):
61
        print arg1
62

63
    @staticmethod
64
    @cython.binding(True)
65
    def mystaticmethod2(a, b, c):
66
        print a, b, c
67

68

69
cdef class SubClass(BaseClass):
70
    """
71
    >>> obj = SubClass()
72
    >>> obj.mystaticmethod(obj, 1)
73
    1
74
    2
75
    >>> SubClass.mystaticmethod(obj, 1)
76
    1
77
    2
78
    """
79

80
    @staticmethod
81
    def mystaticmethod(self, arg1):
82
        print arg1
83
        super().mystaticmethod(self, arg1 + 1)
84

85

86
class SubSubClass(SubClass):
87
    """
88
    >>> obj = SubSubClass()
89
    >>> obj.mystaticmethod(obj, 1)
90
    1
91
    2
92
    3
93
    >>> SubSubClass.mystaticmethod(obj, 1)
94
    1
95
    2
96
    3
97
    """
98

99
    @staticmethod
100
    def mystaticmethod(self, arg1):
101
        print arg1
102
        super().mystaticmethod(self, arg1 + 1)
103

104

105
cdef class ArgsKwargs(object):
106
    @staticmethod
107
    def with_first_arg(arg1, *args, **kwargs):
108
        """
109
        >>> ArgsKwargs().with_first_arg(1, 2, 3, a=4, b=5)
110
        (1, 'pos', 2, 3, ('a', 4), ('b', 5))
111
        """
112
        return (arg1, 'pos') + args + tuple(sorted(kwargs.items()))
113

114
    @staticmethod
115
    def only_args_kwargs(*args, **kwargs):
116
        """
117
        >>> ArgsKwargs().only_args_kwargs()
118
        ()
119
        >>> ArgsKwargs().only_args_kwargs(1, 2, a=3)
120
        (1, 2, ('a', 3))
121
        """
122
        return args + tuple(sorted(kwargs.items()))
123

124
    @staticmethod
125
    def no_args():
126
        """
127
        >>> ArgsKwargs().no_args()
128
        OK!
129
        """
130
        print("OK!")
131

132

133
class StaticmethodSubclass(staticmethod):
134
    """
135
    >>> s = StaticmethodSubclass(None)
136
    >>> s.is_subtype()
137
    True
138
    """
139
    def is_subtype(self):
140
        return isinstance(self, staticmethod)
141

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

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

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

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