Python or Java or maybe PHP?

Alex Martelli aleax at mail.comcast.net
Fri Jan 6 11:05:50 EST 2006


Xavier Morel <xavier.morel at masklinn.net> wrote:
   ...
> Wouldn't it be possible to change the `def` statement to return a 
> reference to the function, and allow omitting the function name thereby
> bypassing the default binding (current behavior)?

It's _possible_ (doesn't  introduce syntax ambiguities) though it does
introduce incompatible interactive-interpreter behavior, as you say:

>  >>> # Extended behavior
>  >>> # returns a reference to the function
>  >>> def foo(*args, **kwargs):
>               pass
> <function at 0x00FA37B0>

This could be avoided if 'def <name><etc>' remained a statement like
today, and a separate expression 'def<etc>' returned a function object
as a result; this would have the aded plus of avoiding the totally new
(to Python) idea of "statement returning a value" (_expressions_ return
a value).

> Note that the function wouldn't "have" it's own name anymore (no more
> "__name__" attribute? Or a blank one?)

Currently, a lambda has a __name__ of '<lambda>'; I'd assume a similar
arrangement if 'expression def' took lambda's place.


> I'm not too sure about the multi line version (and it looks very ugly

Yeah, the multiline's the rub -- there's currently no multiline
expression, and it does look ugly.


>      * May allow for blocks-like constructs (I'm not sure of the current
> state of the closures over Python functions though, these may have to be
> extended to "full" closures if they aren't) and be considered by some as

Python's closures are 'full', but don't allow inner functions to rebind
names in the namespace of outer functions.

I'm not sure a PEP like this has ever been proposed, but the idea of
anonymous def is not new (bar some details of your proposal): if a PEP
doesn't exist, you could write one, at least to firm up all details.


Alex



More information about the Python-list mailing list