[Python-3000] 100% backwards compatible parenless function call statements

Chris Monsanto chris.monsanto at gmail.com
Tue Aug 14 01:52:09 CEST 2007


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.

--Some examples:--

print "Parenless function call!", file=my_file

print(".. but this is still allowed")

# We still need parens for calls to functions where the sole argument is a
tuple
# But you'd have to do this anyway in Python 2.x... nothing lost.
print((1, 2))

# Need parens if the function call isnt the only thing in the statement
cos(3) + 4

# Need parens if function call isnt a statement, otherwise how would we get
the function itself?
x = cos(3)

# Make a the value of my_func...
my_func2 = my_func
my_func2 # call other function
my_func2() # call it again

# Method call?
f = open("myfile")
f.close

# Chained method
obj.something().somethinganother().yeah

--Notes:--

A lot of other things in Python 2.x/Python 3k at the moment have this same
behavior...

# No parens required
x, y = b, a

# But sometimes they are
func((1, 2))

# Generator expressions sometimes don't need parens
func(i for i in list)

# But sometimes they do
func(a, (i for i in list))

--Pros:--

1) Removes unnecessary verbosity for the majority of situations.
2) Python 2.x code works the same unmodified.
3) No weird stuff with non-first class objects, ala Ruby meth.call().
Functions still remain assignable to other values without other trickery.
4) Because it's completely backwards compatible, you could even have
something like from __future__ import parenless in Python 2.6 for a
transition.

--Cons:--

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:
http://www.ferg.org/projects/python_gotchas.html(specifically #5)

I'm willing to write up a proper PEP if anyone is interested in the idea. I
figured I'd poll around first.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20070813/21dbb247/attachment.htm 


More information about the Python-3000 mailing list