31 Mar
2021
31 Mar
'21
6:55 a.m.
A friend, Antoine Rozo, wrote a variant of staticmethod which is callable. With this decorator, it works in A, B and C cases: --- class simplefunction: def __init__(self, func): self.func = func def __get__(self, owner, instance): return self.func def __call__(self, *args, **kwargs): return self.func(*args, **kwargs) @simplefunction def func(): print("my func") class MyClass: method = func func() # A MyClass.method() # B MyClass().method() # C --- It works without the __get__() method, but in this case, we go through the __call__() indirection for A, B and C cases, rather than only going through __call__() indirection in A case. Victor