New submission from Serhiy Storchaka: Special __bytes__ method is purposed for converting to bytes with bytes constructor (as well as __str__ and __float__ for str and float). But this doesn't work if the class is a subclass of str.
>>> class X: ... def __bytes__(self): ... return b'abc' ... >>> bytes(X()) b'abc' >>> class Y(str): ... def __bytes__(self): ... return b'abc' ... >>> bytes(Y()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: string argument without an encoding ---------- components: Interpreter Core files: str___bytes__.patch keywords: patch messages: 255593 nosy: serhiy.storchaka priority: normal severity: normal stage: patch review status: open title: __bytes__ doesn't work for str subclasses type: behavior versions: Python 3.4, Python 3.5, Python 3.6 Added file: http://bugs.python.org/file41192/str___bytes__.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25766> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com