Search code examples
python-3.xparameter-passingdocumentationurllib

What does [x]* mean in python function documention?


I want to specify a timeout parameter for urllib.request.urlopen in python 3.

The docs say:

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

What does [timeout, ]* mean?

I thought that all optional, named arguments (data=None, cafile=None ...) had to appear before all un-named arguments.

It looks here like timeout is an un-named argument. But it appears after data.

Looking just at that documentation, I have no idea whether to use:

  • urlopen(url, 123)
  • urlopen(url, timeout=123)
  • urlopen(url, [123])
  • urlopen(url, [123]*)

I can see from this post that the correct answer is urlopen(url, timeout=123). But if that's the case, why do the docs say [timeout, ]*, why not just timeout=None?


Solution

  • This is a combination of independent features - square brackets and asterisk.

    [timeout, ] means that timeout is optional argument - to simplify you may omit everything that is inside the brackets and the brackets - you will get urllib.request.urlopen(url, data=None, *, cafile=None, capath=None, cadefault=False, context=None).

    * means that all the following arguments are keyword-only arguments - you must specify argument names.