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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -