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

It’s obvious but there is one easy way to shorten the code: using **kwargs. It’s way shorter but the down sides are:
- the “real” function signature gets hidden so IDEs for example won’t pick
This is confusing. One could imagine solving this specific case by having a
These problems could be solved by a decorator that accepts string representation of the signature. The decorator would then have to parse the signature at importing time and set it to the __signature__ attribute on the resultant function. This decorator would also need to bind the arguments e.g. sig.bind(*args, **kwargs), to handle out of order positional arguments. Therefore this would raise an error in the decorator, essentially solving your second point. This would make the example look like this, a lot clearer in my opionion: @signature('''(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None, *, loop=None, executor=None)''') def open(*args, **kwargs): return AiofilesContextManager(_open(*args, **kwargs)) @asyncio.coroutine def _open(*args, loop=None, executor=None, **kwargs): """Open an asyncio file.""" if loop is None: loop = asyncio.get_event_loop() cb = partial(sync_open, *args, **kwargs) f = yield from loop.run_in_executor(executor, cb) return wrap(f, loop=loop, executor=executor) Ben Lewis
participants (1)
-
Ben Lewis