Skip to content

Instantly share code, notes, and snippets.

@niconico25
Created December 26, 2018 12:36
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/939c1e556d7946f4798d078011fd0330 to your computer and use it in GitHub Desktop.
Save niconico25/939c1e556d7946f4798d078011fd0330 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):
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