7

The Python documentation specifies that is is legal to omit the parentheses if a function only takes a single parameter, but

myfunction "Hello!"

generates a syntax error. So, what's the deal?

EDIT:

The statement that I read only applies to generator expressions:

The parentheses can be omitted on calls with only one argument.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • 3
    Are you sure? Can you tell us where do you read that rule? Are you confusing with ruby? – Ruggero Turra May 28 '10 at 22:30
  • 1
    You are probably confusing Python with Ruby or Lua, where parantheses can be omitted sometimes. – interjay May 28 '10 at 22:36
  • 2
    @Jen: they talk about parentheses that denote generator, not the function call. – SilentGhost May 28 '10 at 23:00
  • It is confusing for beginners that some Python words are statements and others are functions, especially as they're moving them around between versions. – Matt Curtis May 28 '10 at 23:33
  • The IPython (http://ipython.scipy.org/moin/) interactive interpreter can omit parentheses, but the `python` interpreter only allows this for keywords (http://docs.python.org/reference/lexical_analysis.html#keywords) – cryo May 29 '10 at 02:14

4 Answers4

8

For your edit:

If you write down a generator expression, like stuff = (f(x) for x in items) you need the brackets, just like you need the [ .. ] around a list comprehension.

But when you pass something from a generator expression to a function (which is a pretty common pattern, because that's pretty much the big idea behind generators) then you don't need two sets of brackets - instead of something like s = sum((f(x) for x in items)) (outer brackets to indicate a function call, inner for the generator expression) you can just write sum(f(x) for x in items)

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
5

You can do it with IPython -- the %autocall magic command controls this feature (as well as the -autocall command line option). Use %autocall 0 to disable the feature, %autocall 1, the default, to have it work only when an argument is present, and %autocall 2 to have it work even for argument-less callables.

In [2]: %autocall 1
Automatic calling is: Smart

In [3]: int '5'
------> int('5')
Out[3]: 5

In [4]: %autocall 2
Automatic calling is: Full

In [5]: int
------> int()
Out[5]: 0
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
2

Without parentheses those wouldn't be functions but statements or keywords (language-intrinsic).

This StackOverflow thread (with some very nice answers) contains a lead as to how one can create their own in pure Python (through advanced hackery, and not a good idea in 99.99% of the cases).

Community
  • 1
  • 1
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
0

As I understand the rule is only about the generator expressions... so for example:

sum(x**2 for x in range(10))

but you would still have to write:

reduce(operator.add, (x**2 for x in range(10)))

This doesn't apply for generic functions though.

wjandrea
  • 28,235
  • 9
  • 60
  • 81