[Python-ideas] parameter omit

Aaron Brady castironpi at comcast.net
Mon May 14 02:32:08 CEST 2007


> -----Original Message-----
> From: Ron Adam [mailto:rrr at ronadam.com]
> Sent: Sunday, May 13, 2007 6:57 PM
> To: Aaron Brady; python-ideas at python.org
> Subject: Re: [Python-ideas] parameter omit
> 
> Aaron Brady wrote:
> [snip]
> 
> You might be able to make a decorator that handles this the way you
> describe.
> 
> 
> It might look something like the following.
> [snip]
> @defaults(None, None, '')
> def f(a, b, c):
>     return a, b, c

Great ideas.  It turned out really nicely.

class GuardDefault:
	Val= object()
	def __call__( self, *args, **kwargs ):
		args=list(args)
		for i,j in enumerate( args ):
			if j is GuardDefault.Val:
				offset=
self.callable.func_code.co_argcount-\
					len(self.callable.func_defaults)
				args[i]=
self.callable.func_defaults[i-offset]
		return self.callable( *args,**kwargs )
	def __init__( self,callable ):
		self.callable= callable

@GuardDefault
def f( a,b=None,c='abc' ):
   print a,b,c

f( 0,GuardDefault.Val,'def' )
f( 0,'somebody',GuardDefault.Val )

gives:

0 None def
0 somebody abc




More information about the Python-ideas mailing list