Created
December 26, 2018 12:36
-
-
Save niconico25/939c1e556d7946f4798d078011fd0330 to your computer and use it in GitHub Desktop.
This file contains 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
class ClassMethod(object): | |
"Emulate PyClassMethod_Type() in Objects/funcobject.c" | |
def __init__(self, f): | |
self.f = f | |
def __get__(self, obj, klass=None): | |
if klass is None: | |
klass = type(obj) | |
def newfunc(*args): | |
return self.f(klass, *args) | |
return newfunc | |
# 公式マニュアルの疑似コードにこれを足してあげれば OK | |
def __call__(self, *args, **kwargs): | |
return self.function(*args, **kwargs) | |
class StaticMethod(object): | |
"Emulate PyStaticMethod_Type() in Objects/funcobject.c" | |
def __init__(self, f): | |
self.f = f | |
def __get__(self, obj, objtype=None): | |
return self.f | |
# 公式マニュアルの疑似コードにこれを足してあげれば OK | |
def __call__(self, *args, **kwargs): | |
return self.function(*args, **kwargs) | |
class C(object): | |
# 第一引数を省略できる。 | |
@StaticMethod | |
def stc_method(): | |
print('Hello, world!') | |
# 第一引数にクラスオブジェクトが代入される。 | |
@ClassMethod | |
def cls_method(cls): | |
print(cls.__name__) | |
o = C() | |
o.stc_method() | |
o.cls_method() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment