The opener parameter of Python 3 open() built-in

Ben Finney ben+python at benfinney.id.au
Tue Sep 4 01:12:48 EDT 2012


Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:

> On 04 Sep 2012 01:13:09 GMT, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> declaimed the following in
> gmane.comp.python.general:
> > What is the rationale for complicating [the builtin] open instead of
> > telling people to just call their opener directly?
>
> 	To avoid the new syntax would mean coding the example as
>
> 	f = os.fdopen(os.open("newfile", flags | os.O_EXCL), "w") 
>
> which does NOT look any cleaner to me... Especially not if "opener" is
> to be used in more than one location.

Exactly. That's not what was asked, though. Steven asked why not call
the opener.

So, having written the opener:

> On Mon, 03 Sep 2012 15:29:05 +0200, Christian Heimes wrote:
> > import os
> > 
> > def opener(file, flags):
> >     return os.open(file, flags | os.O_EXCL)

why not call that directly?

    f = opener(file, flags)

It certainly is cleaner than either of the alternatives so far, and it
doesn't add a parameter to the builtin.


> Furthermore, using "opener" could allow for a localized change to
> affect all open statements in the module -- change file path, open for
> string I/O rather than file I/O, etc.

I don't know of any real-life code which would be significantly improved
by that. Can you point us to some?

-- 
 \      “I find the whole business of religion profoundly interesting. |
  `\     But it does mystify me that otherwise intelligent people take |
_o__)                                    it seriously.” —Douglas Adams |
Ben Finney



More information about the Python-list mailing list