Created
February 6, 2019 04:54
-
-
Save niconico25/588a9f1447bbec99d87a4c7f854c139f 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): | |
... # ここを実装してください。 | |
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): | |
... # ここを実装してください。 | |
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