1

EDIT: This was closed as a duplicate of this post, however as I noted in the OP the usage here clearly not related to list/dictionary unpacking, this is a distinct question.

While reading the docs for the subprocess module I noticed that a number of methods take an * argument. For example, subprocess.run:

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None,
               capture_output=False, shell=False, cwd=None, timeout=None,
               check=False, encoding=None, errors=None, text=None, env=None, 
               universal_newlines=None, **other_popen_kwargs)

I initially interpreted that to mean the preceding positional argument (args in this case) could be repeated any number of times before key-word arguments, but I don't think that is correct. For example, the subprocess.Popen constructor also has an argument referred to only as *, but in that case it follows a keyword argument (pass_fds).

subprocess.Popen(args, bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None,
                 preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None,
                 universal_newlines=None, startupinfo=None, creationflags=0,
                 restore_signals=True, start_new_session=False, pass_fds=(), *, group=None,
                 extra_groups=None, user=None, umask=- 1, encoding=None, errors=None,
                 text=None, pipesize=- 1)

Attempting to search for this just returns results about using *args and **kwargs, and looking at the source code has not elucidated anything to me.

Nick
  • 162
  • 6
  • 1
    keyword-only args – acushner Jul 06 '22 at 04:18
  • 1
    @Aaron as you note the mysterious `*` does not appear in the actual function signature for `subprocess.run`, but it *does* appear in the signature for `subprocess.Popen.__init__`, see [here](https://github.com/python/cpython/blob/3.10/Lib/subprocess.py#L758) , so presumably it is valid. – Nick Jan 17 '23 at 18:05
  • 1
    As I revisit this I am realizing that @acushner's answer is correct, this is explained in detail in [PEP 3102](https://peps.python.org/pep-3102/) – Nick Jan 17 '23 at 18:11
  • 1
    @Nick evidently I was wrong. I will delete my comments to prevent confusion. I had never encountered this notation outside of documentation, and was unaware it was legal python syntax. – Aaron Jan 17 '23 at 22:24
  • 1
    No problem @Aaron! Thanks for taking the time to look at my question! – Nick Jan 17 '23 at 22:29

0 Answers0