Message389896
The OpenWrapper function of io and _pyio is an undocumented hack allowing to use the builtin open() function as a method:
class MyClass:
method = open
MyClass.method(...) # class method
MyClass().method(...) # instance method
It is only needed by the _pyio module: the pure Python implementation of the io module:
---
class DocDescriptor:
"""Helper for builtins.open.__doc__
"""
def __get__(self, obj, typ=None):
return (
"open(file, mode='r', buffering=-1, encoding=None, "
"errors=None, newline=None, closefd=True)\n\n" +
open.__doc__)
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.
"""
__doc__ = DocDescriptor()
def __new__(cls, *args, **kwargs):
return open(*args, **kwargs)
---
The io module simply uses an alias to open:
---
OpenWrapper = _io.open # for compatibility with _pyio
---
No wrapper is needed since built-in functions can be used directly as methods. Example:
---
class MyClass:
method = len # built-in function
print(MyClass.method("abc"))
print(MyClass().method("abc"))
---
This example works as expected, it displays "3" two times.
I propose to simply remove io.OpenWrapper and force developers to explicitly use staticmethod:
class MyClass:
method = staticmethod(open)
io.OpenWrapper is not documented.
I don't understand the remark about dbm.dumb: I fail to see where the built-in open() function is used as a method. |
|
Date |
User |
Action |
Args |
2021-03-31 12:59:21 | vstinner | set | recipients:
+ vstinner |
2021-03-31 12:59:21 | vstinner | set | messageid: <1617195561.83.0.0407283597649.issue43680@roundup.psfhosted.org> |
2021-03-31 12:59:21 | vstinner | link | issue43680 messages |
2021-03-31 12:59:21 | vstinner | create | |
|