0

How can I call a subproccess with a lot of optional parameters?

def subproccess_call(process_name, arg1=None, arg2=None, arg3=None):
  cmd = ['sudo', '%s', '%s', '%s'] % (process_name, arg1, arg2)
  subprocess.Popen(cmd, stdout= subprocess.PIPE,                                  stderr=subprocess.PIPE)

The subproccess method can bei called with zero, one or two additional arguments.

user5580578
  • 1,134
  • 1
  • 12
  • 28
  • Does this answer your question? [Python: defining a function with variable number of arguments](https://stackoverflow.com/questions/45192234/python-defining-a-function-with-variable-number-of-arguments) – MisterMiyagi Mar 28 '20 at 08:44
  • Is there any reason why the arguments aren't passed in directly as a list? – MisterMiyagi Mar 28 '20 at 08:46
  • trying to write clean code - its not desirable to use *kwargs. On the other hand a list will support the open close principle. I'll think about it. – user5580578 Mar 28 '20 at 15:56
  • There is nothing wrong with taking variadic arguments if they are just that. Many python functions do, and the feature exists for a reason. What makes you think variadic arguments are inappropriate here? – MisterMiyagi Mar 28 '20 at 19:07
  • Here is it mentioned as alternative way but not the best way https://docs.python-guide.org/writing/style/ with an example: send('Hello', ['God', 'Mom', 'Cthulhu']). The problem is that you can specify any argument you like so its not clear for the caller of the method – user5580578 Mar 29 '20 at 12:04

0 Answers0