[Python-3000] 'defop' syntax with no new keywords

Talin talin at acm.org
Fri Dec 8 11:15:19 CET 2006

Another mini-brainstorm, which is how to represent PJE's 'defop' syntax 
without adding any keywords:

Alternative 1. Omitting the function name in 'def' statements.

Let us define the behavior of the 'def' statement as follows:

    The function name in the 'def' statement is optional - if the 
function name is absent, then def behaves exactly as it does now, 
*except* that the assignment of the function object to the symbol name 
is skipped. In particular, any function decorators are still invoked, 
however the result of those decorators are thrown away.

So to overload the 'len' operator:

class Foo:

   def (self):
      ... compute len( self ) ...

The idea here is that it's up to the *decorator* to store the function 
object somewhere - we don't automatically store it if there's no 
function name supplied.

In this example, the argument to the 'overload' decorator is a reference 
to the global 'len' generic function. The 'overload' in this case takes 
the reference to the resulting function and adds it as a method to 
'len'. The decorator can extract the type of the function's enclosing 
class from the various attributes of the function object (i.e. the 
function still knows what class defined it.)

The expression 'def (args)' is a no-op unless there are decorators.

Alternative 2: Function name in parens:

In a def statement, putting the name of the function in parentheses 
indicates that the function name is an expression rather than a symbol:

   def (len)(self):

Decorators that are used in this fashion should expect two arguments 
rather than one, which are the name-expression and the function object.

-- Talin

More information about the Python-3000 mailing list