Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28392

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

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: The opener parameter of Python 3 open() built-in
References <k2280c$nf1$1@speranza.aioe.org> <mailman.139.1346678967.27098.python-list@python.org> <504555a5$0$29978$c3e8da3$5496439d@news.astraweb.com> <mailman.167.1346728806.27098.python-list@python.org> <504590c6$0$29977$c3e8da3$5496439d@news.astraweb.com>
Date 2012-09-04 15:45 +1000
Message-ID <87zk56733w.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Steven D'Aprano <steve+comp.lang.python@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

The opener parameter of Python 3 open() built-in Marco <marco_u@nsgmail.com> - 2012-09-03 14:32 +0200
  Re: The opener parameter of Python 3 open() built-in Dave Angel <d@davea.name> - 2012-09-03 09:05 -0400
    Re: The opener parameter of Python 3 open() built-in Marco <marco_u@nsgmail.com> - 2012-09-03 15:33 +0200
    Re: The opener parameter of Python 3 open() built-in Marco <marco.buttu@gmail.com> - 2012-09-03 15:33 +0200
  Re: The opener parameter of Python 3 open() built-in Christian Heimes <lists@cheimes.de> - 2012-09-03 15:29 +0200
    Re: The opener parameter of Python 3 open() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-04 01:13 +0000
      Re: The opener parameter of Python 3 open() built-in Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-03 23:19 -0400
        Re: The opener parameter of Python 3 open() built-in Ben Finney <ben+python@benfinney.id.au> - 2012-09-04 15:12 +1000
          Re: The opener parameter of Python 3 open() built-in Ben Finney <ben+python@benfinney.id.au> - 2012-09-04 15:20 +1000
          Re: The opener parameter of Python 3 open() built-in Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-04 14:44 -0400
        Re: The opener parameter of Python 3 open() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-04 05:25 +0000
          Re: The opener parameter of Python 3 open() built-in Ben Finney <ben+python@benfinney.id.au> - 2012-09-04 15:45 +1000
      Re: The opener parameter of Python 3 open() built-in Serhiy Storchaka <storchaka@gmail.com> - 2012-09-04 15:58 +0300
      Re: The opener parameter of Python 3 open() built-in Terry Reedy <tjreedy@udel.edu> - 2012-09-04 15:16 -0400
      Re: The opener parameter of Python 3 open() built-in Chris Angelico <rosuav@gmail.com> - 2012-09-05 08:18 +1000
      Re: The opener parameter of Python 3 open() built-in Terry Reedy <tjreedy@udel.edu> - 2012-09-04 19:16 -0400
      Re: The opener parameter of Python 3 open() built-in Antoine Pitrou <solipsis@pitrou.net> - 2012-09-06 00:34 +0000
        Re: The opener parameter of Python 3 open() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-06 06:34 +0000
  Re: The opener parameter of Python 3 open() built-in Serhiy Storchaka <storchaka@gmail.com> - 2012-09-03 19:06 +0300
  Re: The opener parameter of Python 3 open() built-in Serhiy Storchaka <storchaka@gmail.com> - 2012-09-04 16:01 +0300

csiph-web