[Python-ideas] Use unbound bytes methods with objects supporting the buffer protocol

Serhiy Storchaka storchaka at gmail.com
Wed Jul 13 12:40:02 EDT 2016


Unbound methods can be used as functions in python. bytes.lower(b) is 
the same as b.lower() if b is an instance of bytes. Many functions and 
methods that work with bytes accept not just bytes, but arbitrary 
objects that support the buffer protocol. Including bytes methods:

     >>> b'a:b'.split(memoryview(b':'))
     [b'a', b'b']

But the first argument of unbound bytes method can be only a bytes instance.

     >>> bytes.split(memoryview(b'a:b'), b':')
     Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
     TypeError: descriptor 'split' requires a 'bytes' object but 
received a 'memoryview'

I think it would be helpful to allow using unbound bytes methods with 
arbitrary objects that support the buffer protocol as the first 
argument. This would allow to avoid unneeded copying (the primary 
purpose of the buffer protocol).

     >>> bytes.split(memoryview(b'a:b'), b':')
     [b'a', b'b']



More information about the Python-ideas mailing list