Is there any operator.unpack in Python?

Is there a built in version for this

def unpack(f, a):
    return f(**a) #or ``return f(*a)''

      

Why not unpacking is considered by the operator and is in the operator. *?

I'm trying to do something similar to this (but of course I want a general solution to the same problem):

from functools import partial, reduce
from operator import add
data = [{'tag':'p','inner':'Word'},{'tag':'img','inner':'lower'}]
renderer = partial(unpack, "<{tag}>{inner}</{tag}>".format)
print(reduce(add, map(renderer, data)))

      

as without using lambda or understanding .

+3


source to share


2 answers


This is not true. What about

print(''.join('<{tag}>{inner}</{tag}>'.format(**d) for d in data))

      

Same behavior in a much more pythonic style.



Edit. Since you don't seem to mind using any nice Python features, how about this:

def tag_format(x):
    return '<{tag}>{inner}</{tag}>'.format(tag=x['tag'], inner=x['inner'])
results = []
for d in data:
    results.append(tag_format(d))
print(''.join(results))

      

+4


source


I don't know of an operator that does what you want, but you don't need that to avoid lambda or understanding:

from functools import reduce
from operator import add
data = [{'tag':'p','inner':'Word'},{'tag':'img','inner':'lower'}]
print(reduce(add, map("<{0[tag]}>{0[inner]}</{0[tag]}>".format, data)))

      



It seems one could generalize something like this if one wanted to.

0


source







All Articles