[Python-Dev] Why can't I encode/decode base64 without importing a module?
Terry Jan Reedy
tjreedy at udel.edu
Tue Apr 23 20:07:39 CEST 2013
On 4/23/2013 12:49 PM, Stephen J. Turnbull wrote:
> Which is an obnoxious API, since (1) you've now made it impossible to
> use "transform" for
> bytestring.transform(from='utf-8', to='iso-8859-1')
> bytestring.transform(from='ulaw', to='mp3')
> textstring.transform(from='rest', to='html')
> without confusion, and (2) the whole world is going to wonder why you
> don't use .encode and .decode instead of .transform and .untransform.
I think the unambiguous solution is to get rid of the notion of
'untransform' (which only means 'transform in the other direction'),
since it requires and presumes an asymmetry that is not always present.
It it precisely the lack of asymmetry in examples like the above that
makes the transform/untransform pair ambiguous as to which is which.
.transform should be explicit and always take two args, no implicit
defaults, the 'from form' and the 'to' form. They can labelled by
position in the natural order (from, to) or by keyword, as in your
examples. For text, the plain undifferentiated form which one might
think of as default could be called 'text' and that for bytes 'bytes'
(as you suggest) or 'ascii' as appropriate.
str.transform would always be unicode to unicode and bytes.transform
always bytes to bytes.
More information about the Python-Dev