1__doc__ = u""" 2>>> class1.plus1(1) 32 4>>> class2.plus1(1) 52 6>>> class3.plus1(1) 72 8>>> class4.plus1(1) 92 10>>> class4().plus1(1) 112 12>>> class4.bplus1(1) 132 14>>> class4().bplus1(1) 152 16""" 17 18cimport cython 19 20def f_plus(a): 21 return a + 1 22 23class class1: 24 plus1 = f_plus 25 26class class2(object): 27 plus1 = f_plus 28 29cdef class class3: 30 plus1 = f_plus 31 32class class4: 33 @staticmethod 34 def plus1(a): 35 return a + 1 36 37 @staticmethod 38 @cython.binding(True) 39 def bplus1(a): 40 return a + 1 41 42 43def nested_class(): 44 """ 45 >>> cls = nested_class() 46 >>> cls.plus1(1) 47 2 48 >>> obj = cls() 49 >>> obj.plus1(1) 50 2 51 """ 52 class class5(object): 53 def __new__(cls): # implicit staticmethod 54 return object.__new__(cls) 55 56 @staticmethod 57 def plus1(a): 58 return a + 1 59 return class5 60 61 62cdef class BaseClass(object): 63 """ 64 Test cdef static methods with super() and Python subclasses 65 66 >>> obj = BaseClass() 67 >>> obj.mystaticmethod(obj, 1) 68 1 69 >>> BaseClass.mystaticmethod(obj, 1) 70 1 71 >>> obj.mystaticmethod2(1, 2, 3) 72 1 2 3 73 >>> BaseClass.mystaticmethod2(1, 2, 3) 74 1 2 3 75 """ 76 77 @staticmethod 78 def mystaticmethod(self, arg1): 79 print arg1 80 81 @staticmethod 82 @cython.binding(True) 83 def mystaticmethod2(a, b, c): 84 print a, b, c 85 86 87cdef class SubClass(BaseClass): 88 """ 89 >>> obj = SubClass() 90 >>> obj.mystaticmethod(obj, 1) 91 1 92 2 93 >>> SubClass.mystaticmethod(obj, 1) 94 1 95 2 96 """ 97 98 @staticmethod 99 def mystaticmethod(self, arg1): 100 print arg1 101 super().mystaticmethod(self, arg1 + 1) 102 103 104class SubSubClass(SubClass): 105 """ 106 >>> obj = SubSubClass() 107 >>> obj.mystaticmethod(obj, 1) 108 1 109 2 110 3 111 >>> SubSubClass.mystaticmethod(obj, 1) 112 1 113 2 114 3 115 """ 116 117 @staticmethod 118 def mystaticmethod(self, arg1): 119 print arg1 120 super().mystaticmethod(self, arg1 + 1) 121 122 123cdef class ArgsKwargs(object): 124 @staticmethod 125 def with_first_arg(arg1, *args, **kwargs): 126 """ 127 >>> ArgsKwargs().with_first_arg(1, 2, 3, a=4, b=5) 128 (1, 'pos', 2, 3, ('a', 4), ('b', 5)) 129 """ 130 return (arg1, 'pos') + args + tuple(sorted(kwargs.items())) 131 132 @staticmethod 133 def only_args_kwargs(*args, **kwargs): 134 """ 135 >>> ArgsKwargs().only_args_kwargs() 136 () 137 >>> ArgsKwargs().only_args_kwargs(1, 2, a=3) 138 (1, 2, ('a', 3)) 139 """ 140 return args + tuple(sorted(kwargs.items())) 141 142 @staticmethod 143 def no_args(): 144 """ 145 >>> ArgsKwargs().no_args() 146 OK! 147 """ 148 print("OK!") 149 150 151class StaticmethodSubclass(staticmethod): 152 """ 153 >>> s = StaticmethodSubclass(None) 154 >>> s.is_subtype() 155 True 156 """ 157 def is_subtype(self): 158 return isinstance(self, staticmethod) 159