python - How to use **kwargs to fill in format parameters automatically -
i want automate/simplyfy this:
def test(stream, tag): subprocess.call('git submodule checkout {stream}-{tag}'.format(stream=stream, tag=tag))
i.e. want rid of stream=stream , tag=tag , somehow make use of **kwargs. possible?
my 2 cents: don't abuse **kwargs
, should used if number of parameters not known a priori.
here approaches not involving **kwargs
:
easy
if concern length of line, can save space using implicit order:
def test(stream, tag): subprocess.call('git submodule checkout {}-{}'.format(stream, tag))
this comes @ price of format string readability, one-liner might it.
object style
wrap parameters in checkout
object:
class checkout: def __init__(self, stream, tag): self.stream = stream self.tag = tag #... def test(checkout): subprocess.call('git submodule checkout {0.stream}-{0.tag}'.format(checkout))
or even:
class checkout: def __init__(self, stream, tag): self.stream = stream self.tag = tag def test(self): subprocess.call('git submodule checkout {0.stream}-{0.tag}'.format(self))
this verbose, checkout
object more simple wrapper, might reused somewhere else or serialized.
Comments
Post a Comment