up vote 4 down vote favorite
share [g+] share [fb]

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)

link|improve this question
2  
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
feedback

2 Answers

up vote 5 down vote accepted

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.

link|improve this answer
It should be "with one or more". +1 for all else. – Jon Purdy Jan 24 '11 at 17:33
@Jon: Thanks for edit! – Michael the Ghost Jan 24 '11 at 17:34
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 the Ghost Jan 25 '11 at 2:35
@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
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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