[Python-ideas] parameter omit

Aaron Brady castironpi at comcast.net
Fri May 11 09:15:37 CEST 2007


> -----Original Message-----
> From: Steven Bethard [mailto:steven.bethard at gmail.com]
> Sent: Friday, May 11, 2007 2:06 AM
> 
> Steven Bethard [mailto:steven.bethard at gmail.com]
> > Can you show me code that would make your new "morpheme" work?  All
> > the implementations I can imagine involve making it some sort of
> > special keyword.
> 
> On 5/11/07, Aaron Brady <castironpi at comcast.net> wrote:
> > Nope, routine object.  I defined it earlier:
> >
> > paramdefault= object()
> 
> Yes, but how is it going to *work*?  Say I wrote the function::
> 
>     def foo(bar=1, baz=2):
>         print bar, baz
> 
> Now if I call that like::
> 
>     foo(bar=paramdefault, baz=paramdefault)
> 
> I'm going to see something like::
> 
>     <object object at 0x009A0468> <object object at 0x009A0468>
> 
> Is that really what you want?  I thought you wanted this to work for
> all functions...
> 
> STeve

You want to see the worked example again.  Voila.

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

default= object()
def call_wrapper( callable, *args, **kwargs ):
   args=list(args)
   for i,j in enumerate( args ):
      if j is default:
         offset= callable.func_code.co_argcount-\
				len(callable.func_defaults)
         args[i]= callable.func_defaults[i-offset]
   return callable( *args,**kwargs )

call_wrapper( f,0,default,'def' )
call_wrapper( f,0,'somebody',default )

#and output is:
0 None def
0 somebody abc






More information about the Python-ideas mailing list