Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.