Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> writes:

> On Mon, 03 Sep 2012 23:19:51 -0400, Dennis Lee Bieber wrote:
> >     f = os.fdopen(os.open("newfile", flags | os.O_EXCL), "w")
> > 
> > which does NOT look any cleaner to me... 
>
> Well, I don't know about that. Once you start messing about with low-
> level O_* flags, it's never going to exactly be clean no matter what you 
> do. But I think a one-liner like the above *is* cleaner than a three-
> liner like the original:
>
> def opener(file, flags):
>     return os.open(file, flags | os.O_EXCL)
>
> open("newfile", "w", opener=opener)
>
> although I accept that this is a matter of personal taste.

If the opener has an unhelpful name like ‘opener’, yes.

But if it's named as any function should be named – to say what it does
that's special – then I think the result would be much clearer::

    outfile = open("newfile", "w", opener=open_exclusive)

> Particularly if the opener is defined far away from where you
> eventually use it.

Another good reason to name helper functions descriptively.

> * or even more Pythonic, expose those numeric modes using strings:
>
>   open(file, 'wx')

Which is, indeed, another improvement in Python 3.3 – the ‘x’ mode for
‘open’ <URL:http://docs.python.org/dev/library/functions.html#open>.

-- 
 \        “The greatest tragedy in mankind's entire history may be the |
  `\       hijacking of morality by religion.” —Arthur C. Clarke, 1991 |
_o__)                                                                  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to