[Python-Dev] Re: Weird io.OpenWrapper hack to use a function as method

2021年3月31日 06:59:19 -0700

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
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/OQUM4RZBIGHF6UJYQDFAMTHOZOU5DLKS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to