args (was Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?))

Michael Spencer mahs at telcopartners.com
Tue Jan 4 17:11:51 EST 2005


Roman Suzi wrote:

> Maybe this is too outlandish, but I see lambdas as a "quote" mechanism,
> which presents a possibility to postpone (precisely control, delegate)
> evaluation. That is, an ovehead for lambda must be much lower but at the
> same time visible to the programmer:
> 
>  d = a + (lambda x, y: x+ y)(3, 4)
[...]

I believe that this "possibility to postpone" divides into two related but 
separate concepts: controlling the moment of evaluation, and assembling the 
arguments required at that moment.  They are both species of 'eval', but 
managing arguments is more specialized, because it includes possibly renaming 
parameters, assigning default values, processing positional and keyword 
arguments, and, perhaps in the future dealing with argument types.

Meanwhile, GvR wrote (about defining Interfaces in the context of Optional 
Static Type Checking)
> Method declarations can be inspected to find out their signature. I propose a
> __signature__ attribute (also for methods defined in classes!) which might be an
> object whose attributes make the signature easily inspectable. This might take 
> the form of a list of argument declaration objects giving the name, type and default
> (if any) for each argument, and a separate argument for the return type. For 
> signatures that include *args and/or **kwds, the type of the additional arguments 
> should also be given (so you can write for example a varargs method whose arguments
> are all strings).

GvR's method.__signature__ object might be related to the args object I proposed 
  as part of the syntax for anonymous functions without 'lambda'. i.e.,

     args(a,*b,**kw) --> an object that specifies but does not evaluate its 
parameters until it is supplied to a callable, possibly with calling parameters

This object would contain the default values, and could contain type 
annotations, explicit, or inferred, as well as more complex assertions used in 
several contexts.

* Current function syntax:
	def func(a,*b,**c) : pass

	creates func with func.__signature__ = args(a,*b,**c)
	and when func is called, the args are evaluated using a mechanism in
	args.__call__
	so, roughly, eval(func.__signature__) --> func.locals


  * Anonymous functions
	Syntax alternatives at http://www.python.org/moin/AlternateLambdaSyntax
	e.g., (f(a) + o(b) - o(c) for args(a, b, c))
	
	args would evaluated with the calling parameters and made available in 			the 
local scope defined by ()
	
  * A stricter alternative to keyword arguments:
  	argspec = args(arg1, arg2, arg3)
	def func(**argspec): pass
	
	is equivalent to def func(arg1, arg2, arg3): pass

	
	args["arg1"]
	
	(i.e., only args defined in argspec are accepted)

  * Useful infrastructure for user-supplied type-based dispatch/lightweight 
multimethods:
	
	argspec = args([(a:int, b:int),(a:str,b:str)])
	
	then a framework can provide a custom args.__call__ method that does
	conformance-checking, adaptation or whatever

	


Michael




More information about the Python-list mailing list