Since Python makes such a distinction between statements and expressions, I am proposing that function calls as statements should be allowed to omit parentheses. What I am proposing is 100% compatible with Python 2.x's behavior of function calls; so those uncomfortable with this (basic) idea can continue to use parens in their function calls. Expressions still require parens because of ambiguity and clarity issues.
<br><br>--Some examples:--<br><br>print "Parenless function call!", file=my_file<br><br>print(".. but this is still allowed")<br><br># We still need parens for calls to functions where the sole argument is a tuple
<br># But you'd have to do this anyway in Python 2.x... nothing lost.<br>print((1, 2)) <br><br># Need parens if the function call isnt the only thing in the statement<br>cos(3) + 4 <br><br># Need parens if function call isnt a statement, otherwise how would we get the function itself?
<br>x = cos(3) <br><br># Make a the value of my_func...<br>my_func2 = my_func <br>my_func2 # call other function<br>my_func2() # call it again<br><br># Method call?<br>f = open("myfile")<br>f.close<br><br># Chained method
<br>obj.something().somethinganother().yeah<br><br>--Notes:--<br><br>A lot of other things in Python 2.x/Python 3k at the moment have this same behavior...<br><br># No parens required<br>x, y = b, a <br><br># But sometimes they are
<br>func((1, 2))<br><br># Generator expressions sometimes don't need parens<br>func(i for i in list)<br><br># But sometimes they do<br>func(a, (i for i in list))<br><br>--Pros:--<br><br>1) Removes unnecessary verbosity for the majority of situations.
<br>2) Python 2.x code works the same unmodified.<br>3) No weird stuff with non-first class objects, ala Ruby meth.call(). Functions still remain assignable to other values without other trickery.<br>4) Because it's completely backwards compatible, you could even have something like from __future__ import parenless in Python
2.6 for a transition.<br><br>--Cons:--<br><br>1) Can't type "func" bare in interpreter to get its repr. I think this is a non-issue; I personally never do this, and with parenless calls you can just type "repr func" anyway. Specifically I think this shouldn't be considered because in scripts doing something like "
f.close" does absolutely nothing and giving it some functionality would be nice. It also solves one of the Python gotchas found here: <a href="http://www.ferg.org/projects/python_gotchas.html">http://www.ferg.org/projects/python_gotchas.html
</a> (specifically #5)<br><br>I'm willing to write up a proper PEP if anyone is interested in the idea. I figured I'd poll around first.<br><br><br>