[Python-ideas] Improving fn(arg=arg, name=name, wibble=wibble) code

Jonathan Fine jfine2358 at gmail.com
Sat Sep 8 07:17:38 EDT 2018


I thank Steve D'Aprano for pointing me to this real-life (although
perhaps extreme) code example

https://github.com/Tinche/aiofiles/blob/master/aiofiles/threadpool/__init__.py#L17-L37
<code>
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
         closefd=True, opener=None, *, loop=None, executor=None):
    return AiofilesContextManager(_open(file, mode=mode, buffering=buffering,
                                        encoding=encoding, errors=errors,
                                        newline=newline, closefd=closefd,
                                        opener=opener, loop=loop,
                                        executor=executor))


@asyncio.coroutine
def _open(file, mode='r', buffering=-1, encoding=None, errors=None,
newline=None,
          closefd=True, opener=None, *, loop=None, executor=None):
    """Open an asyncio file."""
    if loop is None:
        loop = asyncio.get_event_loop()
    cb = partial(sync_open, file, mode=mode, buffering=buffering,
                 encoding=encoding, errors=errors, newline=newline,
                 closefd=closefd, opener=opener)
    f = yield from loop.run_in_executor(executor, cb)

    return wrap(f, loop=loop, executor=executor)
</code>


Anders Hovmöller has proposed a Python syntax extension to improve
this code. It provides, for example
        return wrap(f, *, loop, executor)
as a shorthand for
        return wrap(f, loop=loop, executor=executor)

See: https://mail.python.org/pipermail/python-ideas/2018-September/053207.html

I'd like us, in this thread, to discuss OTHER possible ways of
improving this code. This could include refactoring, and the
introduction of tools. I'm particularly interested in gathering
alternatives, and at this time not much interesting in "knowing which
one is best".

-- 
Jonathan


More information about the Python-ideas mailing list