Python or Java or maybe PHP?

Xavier Morel xavier.morel at masklinn.net
Tue Jan 3 23:09:28 CET 2006


Mike Meyer wrote:
> That doesn't sounds like "hates" to me. More like "doesn't like the
> baggage."
> 
>         <mike

Yet anonymous functions are nice.

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)?

Something along the lines of

 >>> # Current behavior
 >>> def foo(*args, **kwargs):
		pass
 >>> print foo
<function foo at 0x00FA37B0>

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


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

Since functions can already be defined inline, the only thing that'd be 
left would be to end the function's definition when the "wrapper" 
structure ends:

 >>> doSomething(def (*args, **kwargs): pass, arg) # End of the function 
definition at the end of it's argument
 >>> doSomethingElse(def (*args, **kwargs):
...		# Multiline
...		pass
...    )

I'm not too sure about the multi line version (and it looks very ugly 
with a non-monospaced font), but:

Pros (I think):
     * Backwards-compatible (I think, since the new uses of `def` are 
currently errors)
     * Fairly obvious syntax
     * No `lambda` or `macros` baggage, the new form of def would merely 
define an anonymous function instead of a named one.
     * No new keyword, or structure, or idiom
     * Existing idioms are merely slightly extended without changing 
their current meaning

Cons:
     * May reduce readability when misused, and may be used in Very 
Stupid Ways that reduce readability a lot (but then again most construct 
may be abused in some way), e.g.:

         doSomething(arg1, arg2, arg3, def foo(a): manipulate(a)) # 
binds a function to `foo` _and_ sends it to `doSomething`
         ...
         [a few lines of code]
         ...
         foo(value) # where the hell did that "foo" come from?

     * Replaces lambdas with something much more powerful, which may go 
against the goal of getting rid of lambdas (unless the aforementioned 
goal is mostly because of the historical baggage of lambdas/macros)

Unsure:
     * Shows that Python is the Ultimate Language, people are not ready yet.
     * 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 
yielding to the hype (even though the structure itself is more or less 
35 years old)



More information about the Python-list mailing list