Skip to content

Instantly share code, notes, and snippets.

@niconico25
Created February 6, 2019 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niconico25/588a9f1447bbec99d87a4c7f854c139f to your computer and use it in GitHub Desktop.
Save niconico25/588a9f1447bbec99d87a4c7f854c139f to your computer and use it in GitHub Desktop.
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