
On Fri, May 27, 2011 at 2:24 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On Fri, May 27, 2011 at 12:02 PM, INADA Naoki <songofacandy@gmail.com> wrote:
Then, I hope bytes has a fast and efficient "format" method like:
b'{0} {1}'.format(23, b'foo') # accepts int, float, bytes, bool, None 23 foo b'{0}'.format('foo') # raises TypeError for other types. TypeError
What method is invoked to convert the numbers to text?
Doesn't invoke any methods. Please imagine stdio's pritnf.
What encoding is used to convert those numbers to text? How does this operation avoid also converting the *bytes* object to text and then reencoding it?
I've wrote a wrong example.
b'{0} {1}'.format(23, b'foo') # accepts int, float, bytes, bool, None 23 foo
This should be b'23 foo'. Numbers encoded by ascii.
Bytes are not text. Struggling against that is a recipe for making life hard for yourself in Python 3.
I love unicode and use unicode when I can use it. But this is a problem in the real world. For example, Python 2 is convenient for analyzing line based logs containing some different encodings. Python 3
That said, there *may* still be a place for bytes.format(). However, proper attention needs to be paid to the encoding issues, and the question of how arbitrary types can be supported (including how to handle the fast path for existing bytes() and bytearray() objects). The pedagogic cost of making it even harder than it already is to convince people that bytes are not text would also need to be considered.
And line buffering in binary mode is also nice.
The Python 3 IO stack already provides b'\n' based line buffering for binary files.
But the doc says that "1 to select line buffering (only usable in text mode)," http://docs.python.org/dev/library/functions.html#open
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
-- INADA Naoki <songofacandy@gmail.com>