Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: The opener parameter of Python 3 open() built-in References: <504555a5$0$29978$c3e8da3$5496439d@news.astraweb.com> <504590c6$0$29977$c3e8da3$5496439d@news.astraweb.com> X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney Date: Tue, 04 Sep 2012 15:45:39 +1000 Message-ID: <87zk56733w.fsf@benfinney.id.au> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:/4jTL4UhkntFjkFvaUl5AD60gCU= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 43 Organization: Unlimited download news at news.astraweb.com NNTP-Posting-Host: 46f60f4b.news.astraweb.com X-Trace: DXC=XS1KV^?kWSbEW9A[5UK?UNZ[SL`C\KgS0OdZbFg\Fc] Xref: csiph.com comp.lang.python:28392 Steven D'Aprano 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’ . -- \ “The greatest tragedy in mankind's entire history may be the | `\ hijacking of morality by religion.” —Arthur C. Clarke, 1991 | _o__) | Ben Finney