[Python-ideas] duck typing for io write methods

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Fri Jun 14 09:54:28 CEST 2013


Greg Ewing <greg.ewing at ...> writes:

> 
> 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.
> 

Hi Greg,
aren't the I/O objects in io inheriting from each other anyway? So changes
in the appropriate base classes would be reflected in subclasses?
Best,
Wolfgang





More information about the Python-ideas mailing list