Do we need _pyio at all? Does PyPy or any other Python implementation use it? On Wed, Mar 31, 2021 at 9:36 PM Victor Stinner <vstinner@python.org> wrote:
Hi,
The io module provides an open() function. It also provides an OpenWrapper which only exists to be able to store open as a method (class or instance method). In the _pyio module, pure Python implementation of the io module, OpenWrapper is implemented as:
class OpenWrapper: """Wrapper for builtins.open
Trick so that open won't become a bound method when stored as a class variable (as dbm.dumb does).
See initstdio() in Python/pylifecycle.c. """ def __new__(cls, *args, **kwargs): return open(*args, **kwargs)
I would like to remove this class which is causing troubles in the PEP 597 implementation, but I don't know how. Simplified problem: --- def func(): print("my func")
class MyClass: method = func
func() # A MyClass.method() # B obj = MyClass() obj.method() # C ---
With this syntax, A and B work, but C fails with TypeError: func() takes 0 positional arguments but 1 was given.
If I decorate func() with @staticmethod, B and C work, but A fails with TypeError: 'staticmethod' object is not callable.
Is OpenWrapper the only way to have a callable object which works in the 3 variants A, B and C?
A, B and C work if MyClass is modified to use staticmethod:
class MyClass: method = staticmethod(func)
Victor -- Night gathers, and now my watch begins. It shall not end until my death. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/QZ7SFW3I... Code of Conduct: http://python.org/psf/codeofconduct/
-- Inada Naoki <songofacandy@gmail.com>