This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gvanrossum
Recipients barry, belopolsky, benjamin.peterson, cben, eric.araujo, ezio.melotti, flox, georg.brandl, gvanrossum, jcea, lemburg, loewis, ncoghlan, pconnell, petri.lehtinen, r.david.murray, ssbarnea, vstinner
Date 2013-04-23.15:42:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366731751.42.0.951319015881.issue7475@psf.upfronthosting.co.za>
In-reply-to
Content
str.decode() and bytes.encode() are not coming back.

Any proposal had better take into account the API design rule that the *type* of a method's return value should not depend on the *value* of one of the arguments.  (The Python 2 design failed this test, and that's why we changed it.)

It is however fine to let the return type depend on one of the argument *types*.  So e.g. bytes.transform(enc) -> bytes and str.transform(enc) -> str are fine.  And so are e.g. transform(bytes, enc) -> bytes and transform(str, enc) -> str.  But a transform() taking bytes that can return either str or bytes depending on the encoding name would be a problem.

Personally I don't think transformations are so important or ubiquitous so as to deserve being made new bytes/str methods.  I'd be happy with a convenience function, for example transform(input, codecname), that would have to be imported from somewhere (maybe the codecs module).

My guess is that in almost all cases where people are demanding to say e.g.

  x = y.transform('rot13')

the codec name is a fixed literal, and they are really after minimizing the number of imports.  Personally, disregarding the extra import line, I think

  x = rot13.transform(y)

looks better though.  Such custom APIs also give the API designer (of the transformation) more freedom to take additional optional parameters affecting the transformation, offer a set of variants, or a richer API.
History
Date User Action Args
2013-04-23 15:42:31gvanrossumsetrecipients: + gvanrossum, lemburg, loewis, barry, georg.brandl, jcea, cben, ncoghlan, belopolsky, vstinner, benjamin.peterson, ezio.melotti, eric.araujo, r.david.murray, ssbarnea, flox, petri.lehtinen, pconnell
2013-04-23 15:42:31gvanrossumsetmessageid: <1366731751.42.0.951319015881.issue7475@psf.upfronthosting.co.za>
2013-04-23 15:42:31gvanrossumlinkissue7475 messages
2013-04-23 15:42:31gvanrossumcreate