Pergunta

I was experimenting with something on the Python console and I noticed that it doesn't matter how many spaces you have between the function name and the (), Python still manages to call the method?

>>> def foo():
...   print 'hello'
...
>>> foo ()
hello
>>> foo                ()
hello

How is that possible? Shouldn't that raise some sort of exception?

Foi útil?

Solução

From the Lexical Analysis documentation on whitespace between tokens:

Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separate tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

Inverting the last sentence, whitespace is allowed between any two tokens as long as they should not instead be interpreted as one token without the whitespace. There is no limit on how much whitespace is used.

Earlier sections define what comprises a logical line, the above only applies to within a logical line. The following is legal too:

result = (foo
                 ())

because the logical line is extended across newlines by parenthesis.

The call expression is a separate series of tokens from what precedes; foo is just a name to look up in the global namespace, you could have looked up the object from a dictionary, it could have been returned from another call, etc. As such, the () part is two separate tokens and any amount of whitespace in and around these is allowed.

Outras dicas

You should understand that

foo()

in Python is composed of two parts: foo and ().

The first one is a name that in your case is found in the globals() dictionary and the value associated to it is a function object.

An open parenthesis following an expression means that a call operation should be made. Consider for example:

def foo():
    print "Hello"

def bar():
    return foo

bar()()  # Will print "Hello"

So they key point to understand is that () can be applied to whatever expression precedes it... for example mylist[i]() will get the i-th element of mylist and call it passing no arguments.

The syntax also allows optional spaces between an expression and the ( character and there's nothing strange about it. Note that also you can for example write p . x to mean p.x.

Python takes many cues from the C programming language. This is certainly one of them. Consider the fact that the following compiles:

int add(int a, int b)
{
    return a + b;
}

int main(int argc, char **argv)
{
    return add        (10, 11);
}

There can be an arbitrary number of spaces between the function name and the arguments to it.

This also holds true in other languages. Consider Ruby, for example,

def add(a, b)
  a + b
end

add       (10, 11)

Sure you get a warning, but it still works and although I don't know how, I'm sure that warning could easily be suppressed.

Perfectly fine. Leading spaces or not, anything between a name of a symbol of sorts and rounded braces would probably change the name of the symbol (in this case a function definition) in question.

This is also common in various C-based languages, where padding spaces between the end of a function name and the parameter list can be done without issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top