Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists as arguments
Another stupidly simply question, but I have to ask anyway: why do some functions expect their arguments to be what I assume to be lists (square brackets)?
So for instance the bytes function expects the arguments to be written between brackets.
Another example would be the run function of the subprocess module:['ls', '-l'])
And how can one infer this by reading only its definition, and not the examples?

This doesn't seem very helpful in this respect:
Quote:, *, 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)
Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.
You can't infer it from the definition unless it contains hints (which are optional). A function can be validly called with an int or a list passed as one of the arguments, but the function may not handle both cases properly.

Generally you will use the documentation for what to pass in.

Let's start with bytes. The help for it shows...:

Quote:Help on class bytes in module builtins:

class bytes(object)
| bytes(iterable_of_ints) -> bytes

Okay, so for the single argument call, it wants that argument to be an iterable of ints. A list of ints would be fine (as your example), a bare int would not.

When you look at subprocess, it tells you it wants the same args as Popen. Looking at Popen, it tells us the first argument is:

Quote: | Arguments:
| args: A string, or a sequence of program arguments.

So this function can take a string (and it will try to parse the string) or a sequence (and it will treat each element as a separate execution argument).
Functions do no expect arguments in square brackets. A function may expect a list as an argument.
def reverse_list(src_list):
    return [src_list[a] for a in range(len(src_list)-1, -1, -1)]

x = reverse_list([1, 2, 3, 4, 5])
The function reverse_list() expects a list. [1, 2, 3, 4, 5] is a list. x is also a list.

When I type help( I get this:
Quote:Help on function run in module subprocess:

run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs)
Run command with arguments and return a CompletedProcess instance.

The returned instance will have attributes args, returncode, stdout and
stderr. By default, stdout and stderr are not captured, and those attributes
will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.

If check is True and the exit code was non-zero, it raises a
CalledProcessError. The CalledProcessError object will have the return code
in the returncode attribute, and output & stderr attributes if those streams
were captured.

If timeout is given, and the process takes too long, a TimeoutExpired
exception will be raised.

There is an optional argument "input", allowing you to
pass bytes or a string to the subprocess's stdin. If you use this argument
you may not also use the Popen constructor's "stdin" argument, as
it will be used internally.

By default, all communication is in bytes, and therefore any "input" should
be bytes, and the stdout and stderr will be bytes. If in text mode, any
"input" should be a string, and stdout and stderr will be strings decoded
according to locale encoding, or by "encoding" if set. Text mode is
triggered by setting any of text, encoding, errors or universal_newlines.

The other arguments are the same as for the Popen constructor.

If that isn't enough information I look at the python documentation:

The manual page has this gem:

Quote:args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 2,120 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020