views:

155

answers:

5

Redo:

(Shorter version just for Stephen202)

What is the significance in putting the comma after after the open bracket, instead of before? And, what is the significance of nesting the brackets.

How they are:

RegexObject.match(string[, pos[, endpos]])

What I would expect:

RegexObject.match(string, [pos], [endpos])

WHY ARE THE BRACKETS NESTED INSTEAD OF JUST LIKE THIS:

RegexObject.match(string[, pos][, endpos])

Why can you back the first bracket into the required argument and that works, but then backing the second bracket into the first is just bletch? This nested nature is what confuses me the most, and not one out of 7 answers has answered that. cvondrick gets the nugget because at lest he answered the comma part and now I understand that.

+9  A: 

The square bracket means that the contents are optional, but everything outside of square brackets is compulsory.

With your notation:

RegexObject.match(string, [pos], [endpos])

I would expect to have to write:

r.match("foo",,)

The nesting is required because if you supply the third parameter then you must also supply the second parameter even though it is an optional parameter. The following non-nested alternative would be ambiguous:

RegexObject.match(string[, pos][, endpos])
Mark Byers
Very nicely explained, on both points.
Will McCutchen
Aha!!!! Thanks. That's what I thought which I mentioned in my previous version of the question http://stackoverflow.com/questions/2119926/why-do-python-functions-show-arguments-as-nested-lists-in-the-documentation-clo mentioned, and nobody got it, so they closed it!! lol. I'm glad there's alot of smart people on here too or I'd pull my hair out after comments like those on my last post. I suppose it was my fault to assume they'd only read the title.
orokusaki
+2  A: 

The open bracket indicates an optional argument. If the comma were outside the bracket, you would have to type it even if you didn't want to use the pos argument (for example).

Hank Gay
+2  A: 

Because otherwise the correct syntax would be to include the comma even if you are ignoring the arguments. The parts inside the square brackets are optional, so by moving the commas out of the square brackets, they are no longer optional. For example, to call the function below with only a string:

RegexObject.match(string, [pos], [endpos])

I would have to do:

RegexObject.match("foobar",,)

But, that isn't very elegant.

carl
A: 

If you think about the brackets enclosing all optional components of the argument list, it makes more sense. Essentially, anything inside brackets may be left out at the discretion of the user.

Eric
A: 

The brackets mean that you can leave out the part between them. So if the docs would be written the way you suggest, it would imply that you can write RegexObject.match(string,,) by leaving everything in brackets. Or RegexObject.match(string,,endpos) by just leaving out the second one. But you can't. If you leave out endpos, you also have to leave out the comma before it. And if you leave out pos, you have to leave out the comma before it as well as endpos. So it's written in a way that makes that clear.

sepp2k