elegant unpacking variable-length tuples

半世苍凉 提交于 2019-12-10 14:34:18

问题


A real, if silly problem:

https://github.com/joshmarshall/tornadorpc/blob/master/tornadorpc/base.py

def start_server(handlers, ...):
    ...
    for (route, handler) in handlers:
        ...

Normally "handlers" is a list of 2-element tuples. But with this particular solution (Tornado) you can pass a third argument to a particular handler (kw args). So a tuple in "handlers" may have 2 elems sometimes or 3 elems other times.

I need to unpack this in a loop. Sure, I can do smth like length checking or try..except on unpacking. Ugh.

Can you think of smth better/more clever than this:

In [8]: handlers
Out[8]: [(1, 2), (3, 4, 5), (6, 7)]


In [9]: new_handlers = [x + (None,) for x in handlers]

?


回答1:


If that handler takes keyword arguments, then use a dictionary for the third element:

handlers = [(1, 2, {}), (3, 4, {'keyword': 5), (6, 7, {})]

for route, handler, kwargs in handlers:
    some_method(route, handler, **kwargs)

Or you can apply the arguments using *args syntax; in that case just catch all values in the loop:

for args in handlers:
    some_method(*args)

If you have to unpack into at least 2 arguments, do so in a separate step:

for handler in handlers:
    route, handler, args = (handler[0], handler[1], handler[2:])

where args would be a tuple of 0 or more elements.

In Python 3, you can handle arbitrary width unpacking with a splat (*) target:

for route, handlers, *args in handlers:

where *args captures 0 or more extra values in the unpack.

The other route, to elements in handlers to a minimal length could be done with:

[(h + (None,) * 3)[:3] for h in handlers]

Demo:

>>> handlers = [(1, 2), (3, 4, 5), (6, 7)]
>>> [(h + (None,) * 3)[:3] for h in handlers]
[(1, 2, None), (3, 4, 5), (6, 7, None)]



回答2:


Since Python 3 you can use PEP 3132 extended unpacking:

for route, handler, *kw in handlers:
    ...

If that isn't an option, unpack with an intermediary:

for handler in handlers:
    (route, handler), kw = handler[:2], handler[2:]


来源:https://stackoverflow.com/questions/17967426/elegant-unpacking-variable-length-tuples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!