Last active
April 13, 2019 16:20
-
-
Save lagagain/d9aa1d73cc6183afebdc2e94928a5e22 to your computer and use it in GitHub Desktop.
classmethod and staticmethod implement for python3.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def myClassmethod(m, *arg, **karg): | |
| def _m(cls, *arg, **karg): | |
| cls = cls if isinstance(cls, type(int)) else cls.__class__ | |
| return m(cls, *arg, **karg) | |
| return _m | |
| def myStaticmethod(m, *arg, **karg): | |
| def _m(*arg, **karg): | |
| if len(arg) > 0: | |
| if m.__name__ in dir(arg[0].__class__): | |
| return getattr(arg[0].__class__, m.__name__)(*arg[1:], **karg) | |
| return m(*arg, **karg) | |
| return _m | |
| print("built-in test start") | |
| class C1: | |
| a = 1 | |
| b = 2 | |
| c = 3 | |
| def s1a(self, a): | |
| self.a = a | |
| return self.a | |
| def g1a(self): | |
| return self.a | |
| @classmethod | |
| def s2a(cls, a): | |
| cls.a = a | |
| return cls.a | |
| @classmethod | |
| def g2a(cls, ): | |
| return cls.a | |
| @staticmethod | |
| def s(string = "Hello, World"): | |
| return string | |
| def r1(self): | |
| return self | |
| @classmethod | |
| def r2(cls): | |
| return cls | |
| I1 = C1() | |
| assert I1.g1a() == 1, "init error" | |
| assert I1.a == 1, "init error" | |
| assert C1.a == 1, "Class a error" | |
| assert I1.s1a(9) == 9, "set a fail" | |
| assert I1.s2a(9) == 9, "set class a fail" | |
| assert I1.g1a() == 9, "a not correct, need 9 , because setted" | |
| assert I1.g2a() == 9, "class a not correct, need 9 , because setted" | |
| assert I1.a == 9, "a not correct, need 9 , because setted" | |
| assert I1.__class__.a == 9, "class a not correct, need 9 , because setted" | |
| assert C1.a == 9, "class a not correct, need 9 , because setted" | |
| assert I1.s() == "Hello, World", "static method error" | |
| assert C1.s() == "Hello, World", "class static method error" | |
| assert I1.s("Hi") == "Hi", "static method error" | |
| assert C1.s("Hi") == "Hi", "class static method error" | |
| print("built-in test end ") | |
| print() | |
| print("implement test start") | |
| class C2: | |
| a = 1 | |
| b = 2 | |
| c = 3 | |
| def s1a(self, a): | |
| self.a = a | |
| return self.a | |
| def g1a(self): | |
| return self.a | |
| @myClassmethod | |
| def s2a(cls, a): | |
| cls.a = a | |
| return cls.a | |
| @myClassmethod | |
| def g2a(cls, ): | |
| return cls.a | |
| @myStaticmethod | |
| def s(string = "Hello, World"): | |
| return string | |
| def r1(self): | |
| return self | |
| @myClassmethod | |
| def r2(cls): | |
| return cls | |
| I2 = C2() | |
| assert I2.g1a() == 1, "init error" | |
| assert I2.a == 1, "init error" | |
| assert C2.a == 1, "Class a error" | |
| assert I2.s1a(9) == 9, "set a fail" | |
| assert I2.s2a(9) == 9, "set class a fail" | |
| assert I2.g1a() == 9, "a not correct, need 9 , because setted" | |
| assert I2.g2a() == 9, "class a not correct, need 9 , because setted" | |
| assert I2.a == 9, "a not correct, need 9 , because setted" | |
| assert I2.__class__.a == 9, "class a not correct, need 9 , because setted" | |
| assert C2.a == 9, "class a not correct, need 9 , because setted" | |
| assert I2.s() == "Hello, World", "static method error" | |
| assert C2.s() == "Hello, World", "class static method error" | |
| print("implement test end ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def A(end="\n"): | |
| print("Hello", end=end) | |
| return "Hello" | |
| assert A() == "Hello", "define A error" | |
| def B(f): | |
| def _f(): | |
| r = f("") | |
| print(", World") | |
| return r + ", World" | |
| return _f | |
| def wrap(f1, f2): | |
| G = globals() | |
| f1_name = f1.__name__ | |
| G[f1_name] = f2(f1) | |
| wrap(A,B) | |
| assert A() == "Hello, World", "wrap A to B(A) fail" | |
| class C: | |
| def A(): | |
| print("Ha Ha Ha") | |
| return "Ha Ha Ha" | |
| @staticmethod | |
| def useA(): | |
| G = globals() | |
| return G["A"]() | |
| assert C.A() == "Ha Ha Ha", "shall call class method" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment