[Python-ideas] duck typing for io write methods

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jun 14 02:30:31 CEST 2013


Wolfgang Maier wrote:
> the write methods of the different
> io module objects work on bytes and str objects only. The built-in functions
> print() and bytes(), on the other hand, use an arbitrary object's __str__
> and __bytes__ methods to compute the str and bytes they should work with.
> Wouldn't it be more consistent and pythonic if the io write methods behaved
> the same way?

There's a difference: print() and bytes() are just single
functions, but write() is an interface implemented by many
objects. Requiring write() to apply __str__ or __bytes__
would place a burden on all implementations of I/O objects.

-- 
Greg


More information about the Python-ideas mailing list