I read the Python documentation a lot and sometimes I am baffled by this notation:

os.path.join(path1[, path2[, ...]])

I somehow make that [, path[,...]] is a list but I would like to know if I am reading it correctly.

Bear with me, this is coming from a Java developer who is trying out Python. X)

  • 4
    If you ever encountered BNF notation, you remember that it puts optional parts into [square brackets]. – 9000 Jan 24 '11 at 17:47
  • What does BNF use for literal square brackets? – dan04 Jan 25 '11 at 6:08

That is for multiple arguments. You could call that method with 1 or more variables. That particular method could be called with:

  1. join(path1)
  2. join(path1, path2)
  3. join(path1, path2, <optional parameters>)

Option 3 can only be used when the path2 argument is present. If you have have use C, think printf("Number %d", number);

According to the python documentation, those optional parameters are for more paths. So you could call join(path1, path2, path3, path4) or with as many paths as you like.

  • It should be "with one or more". +1 for all else. – Jon Purdy Jan 24 '11 at 17:33
  • But can the parameters be passed, say, in one list? – Jeune Jan 24 '11 at 19:33
  • @Jeune: I don't think so, but I'm not a python expert by any strech. – Michael K Jan 25 '11 at 2:35
  • 1
    @Jeune: you can use *args, to unpack a list as arguments to a function. ie os.path(*[arg1, arg2, arg3]), or if args was already a list. – Matthew Schinckel Jan 26 '11 at 7:39

The brackets indicate an optional parameter. The ellipses indicate a variable-length argument list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.