[Python-Dev] Why can't I encode/decode base64 without importing a module? (original) (raw)

Guido van Rossum guido at python.org
Tue Apr 23 19:24:53 CEST 2013


On Tue, Apr 23, 2013 at 9:04 AM, M.-A. Lemburg <mal at egenix.com> wrote:

On 23.04.2013 17:47, Guido van Rossum wrote:

On Tue, Apr 23, 2013 at 8:22 AM, M.-A. Lemburg <mal at egenix.com> wrote:

Just as reminder: we have the general purpose encode()/decode() functions in the codecs module:

import codecs r13 = codecs.encode('hello world', 'rot-13') These interface directly to the codec interfaces, without enforcing type restrictions. The codec defines the supported input and output types. As an implementation mechanism I see nothing wrong with this. I hope the codecs module lets you introspect the input and output types of a codec given by name? At the moment there is no standard interface to access supported input and output types... but then: regular Python functions or methods also don't provide such functionality, so no surprise there ;-)

Not quite the same though. Each function has its own unique behavior. But codecs support a standard interface, except that the input and output types sometimes vary.

It's mostly a matter of specifying the supported type combinations in the codec documentation.

BTW: What would be a use case where you'd want to programmatically access such information before calling the codec ?

As you know, in Python 3, most code working with bytes doesn't also work with strings, and vice versa (except for a few cases where we've gone out of our way to write polymorphic code -- but users rarely do so, and any time you use a string or bytes literal you basically limit yourself to that type).

Suppose I write a command-line utility that reads a file, runs it through a codec, and writes the result to another file. Suppose the name of the codec is a command-line argument (as well as the filenames). I need to know whether to open the files in text or binary mode based on the name of the codec.

-- --Guido van Rossum (python.org/~guido)



More information about the Python-Dev mailing list