[Python-ideas] duck typing for io write methods

Antoine Pitrou solipsis at pitrou.net
Fri Jun 14 10:53:07 CEST 2013


On Thu, 13 Jun 2013 08:50:21 +0000 (UTC)
Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de>
wrote:
> Dear all,
> currently - and referring to Python 3 - 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?

No, it wouldn't. print() is meant to print *any* object's string
representation, write() is meant to write a str-like or bytes-like
object to an output stream. It isn't the same thing and therefore there
is no "consistency" to look after here.

Having write() convert implicitly all arguments to bytes or str would
not be "Pythonic", it would be PHP-ic.

Regards

Antoine.




More information about the Python-ideas mailing list