[Tutor] ?4uplz-> function

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 8 Oct 2001 23:31:21 -0700 (PDT)


On Fri, 5 Oct 2001, David Meltzer wrote:

> i would like to find a way to create a def that behaves more like a
> builtin keyword such as 'print' in that i would not need to use a
> parenthesis.
> 
> here is an example
> 
> print 10 //this is legal
> 
> def printit(Num):
> 	print Num
> 
> printit (10) //legal
> printit 10 //illegal.
> 
> so perhaps you know a way to make the illegal legal

No; in Python, that sort of syntax is reserved only for the builtins.

I think the decision to force us to use parentheses on function calls is
to simplify the rules: if there's just one way to interpret something,
there's less room for misinterpretation.

Others have given some examples of where misinterpretation might occur.  
Even if we somehow cook up a set of rules rules so that Python doesn't get
confused, many of us certainly would!  *grin*

I think it's a deliberate design decision to make parens mandatory for
function calls.  Because of this single rule, other parts of the Python
language are easier to explain.  For example: we can easily say that, in
Python, parens are the magic symbols we use to make functions fire off,
and that without them, functions stay dormant as data that can be passed
off to other functions:

###
>>> def printit(num): print num
...
>>> printit                        ## What happens if we forget the
<function printit at 0x80e2054>    ## parens?  Ah.  It's a function.
>>> def playItAgain(sam):
...     sam(1)
...     sam(1)
...
>>> playItAgain(printit)
1
1
###

With your proposal, we'd have to interpret:

###
>>> printit
###

as calling printit with no arguments.  But then, how do we pass functions
off to other functions? Then we'd have to invent something like a
referencing operator, and then Python suddenly wouldn't be as nice to work
with.  Lots of implications from a "simple" syntax change.  I'm glad I'm
not a language designer.  *grin*

Hope this helps!