I use an array for storage of functions which needs to be called in a row:
def do_xslt(xsl, xml):
newxml = dosomethingwithit(xsl,xml)
return newxml
TRANSFORM_PIPELINE = [
do_xslt('pass1.xsl'),
do_xslt('pass2.xsl'),
do_xslt('pass3.xsl'),
]
what I now want to do is to call the TRANSFORM_PIPELINE with its given argument and a dynamic one.
I have something like this in my mind which should call in a loop do_xslt('passX.xsl', xml)
for transform in TRANSFORM_PIPELINE:
xml = transform(xml)
This approach is of course wrong. But how to make the right way in Python?
Use
functools.partial()to partially apply the function:Calling the functions returned by
partial()will calldo_xslt('pass1.xsl', *args, **kwargs)with*argsand**kwargsbeing the arguments passed to the new function.Demo: