inspect feature

Aaron "Castironpi" Brady castironpi at gmail.com
Tue Oct 14 17:00:24 EDT 2008


On Oct 14, 2:32 pm, George Sakkis <george.sak... at gmail.com> wrote:
> On Oct 14, 2:35 pm, "Aaron \"Castironpi\" Brady"
>
>
>
> <castiro... at gmail.com> wrote:
> > On Oct 14, 9:42 am, George Sakkis <george.sak... at gmail.com> wrote:
>
> > > On Oct 14, 3:06 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > > wrote:
>
> > > > En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
> > > > <castiro... at gmail.com> escribió:
>
snip
> > > > > You are wrapping a function with this signature:
>
> > > > > def f( a, b, c= None, *d, **e ):
>
> > > > > You want to find out the values of 'a', 'b', and 'c' in a decorator.
> > > > > You have these calls:
>
> > > > > f( 0, 1, 'abc', 'def', h= 'ghi' )
> > > > > f( 0, 1 )
> > > > > f( 0, 1, h= 'abc' )
> > > > > f( 0, 1, 'abc', c= 'def' ) #raise TypeError: multiple values
>
> > > > > How do you determine 'a', 'b', and 'c'?
>
> > > > I'm afraid you'll have to duplicate the logic described here:  http://docs.python.org/reference/expressions.html#id9
> > > > To my knowledge, there is no available Python code (in the stdlib or
> > > > something) that already does that.
>
> > > I wrote such a beast some time ago; it's hairy but to the best of my
> > > knowledge it seems to reproduce the standard Python logic:http://code.activestate.com/recipes/551779/
>
> > > George
>
> > I didn't see a 'got a duplicate argument for keyword "d"' error, but I
> > can add one if I need to.
>
> Why don't you try it out:
>
> >>> def f( a, b, c= None, *d, **e ): pass
> >>> getcallargs(f, 0, 1, 'abc', c= 'def' )
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "getcallargs.py", line 53, in getcallargs
>     "argument '%s'" % (f_name,arg))
> TypeError: f() got multiple values for keyword argument 'c'
>
> George

Excellent.

Here's some more info.

Ver 2.5:

>>> f( c= 0, c= 0 )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'c'
>>> getcallargs( f, c= 0, c= 0 )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 64, in getcallargs
TypeError: f() takes at least 2 non-keyword  arguments (0 given)

Just the wrong order to check errors in.  Note the spacing '..keyword
arguments..'.  Not a problem on 2.6:

Ver 2.6:

>>> f( c= 0, c= 0 )
  File "<stdin>", line 1
SyntaxError: keyword argument repeated
>>> getcallargs( f, c= 0, c= 0 )
  File "<stdin>", line 1
SyntaxError: keyword argument repeated

+1 standard library.



More information about the Python-list mailing list