[I18n-sig] Modified open() builtin (Re: Python Character Model)
Sun, 11 Feb 2001 23:47:46 +0100
Paul Prescod wrote:
> "M.-A. Lemburg" wrote:
> > Paul Prescod wrote:
> > >
> > > ...
> > > There is an important reason that we did not use a keyword argument.
> > >
> > > We (at least some subset of the people in the i18n-sig) want every
> > > single new instance of the "open" function to declare an encoding.
> > This doesn't make sense: not all uses of open() target text
> > information. What encoding information would you put into an
> > open() which wants to read a JPEG image from a file ?
> "binary" or "raw"
I'm -1 on enforcing this. Encoding is optional and has to be, since
1. existing programs don't provide the parameter and would break
2. the user can't know in advance if the file to be opened is
of a certain encoding or type (e.g. image or sound file)
3. not all files contain encoded data for which Python provides
a codec for decoding
4. it may not be in the programers intent to have the file
decoded even though it uses a certain encoding
> > f = open(filename, 'w', encoding='mbcs')
> > There's a little more typing required, but the readability is
> > unbeatable...
> Why not go all the way:
> f = open(filename=filename, mode='w', encoding='mbcs')
Now you're being silly...
> Keyword attributes are great for optional parameters. I don't see
> encoding as optional. Anyhow, I like Fredrick's idea of extending the
> mode string.
I'm not sure I like it -- it looks like a hack to me and
I don't really see what's so bad about an optional keyword
argument for open(). At least noone has yet convinced me
of any problems with it.
Note that Fredrik's idea doesn't make the encoding parameter
a requirement either (and this is Goodness).
Python Pages: http://www.lemburg.com/python/