[Python-Dev] (no subject)

Brett C. bac at OCF.Berkeley.EDU
Mon Mar 14 20:42:35 CET 2005

Phillip J. Eby wrote:
> One solution is to have a __signature__ attribute that's purely 
> documentary.  That is, modifying it wouldn't change the function's 
> actual behavior, so it could be copied with update_meta() but then 
> modified by the decorator if need be.  __signature__ would basically be 
> a structure like the one returned by inspect.getargspec().  Also, 
> 'instancemethod' would have a __signature__ equal to its 
> im_func.__signature__ with the first argument dropped off, thus making 
> it easy to introspect wrapper chains.
> I discussed this approach with Guido in private e-mail a few months back 
> during discussion about an article I was writing for DDJ about 
> decorators.  We also discussed something very similar to 
> 'update_meta()', but never settled on a name.  Originally he wanted me 
> to PEP the whole thing, but he wanted it to include optional type 
> declaration info, so you can probably see why I haven't done anything on 
> that yet.  :)
> However, if we can define a __signature__ format that allows for type 
> declaration, I imagine there'd be little problem with moving forward on it.

It could be as simple as just a bunch of tuples.  The following (assuming *args 
and **kwargs are not typed; don't remember if they can be)::

   def foo(pos1, pos2:int, key1="hi", key2=42:int, *args, **kwargs): pass

could be::

   ((('pos1', None), ('pos2', int)), (('key1', "hi", None), ('key2', 42, int)),
    'args', 'kwargs')

In case the format is not obvious, just a bunch of tuples grouped based on 
whether they are positional, keyword, or one of the collecting arguments.  For 
positional arguments, have a two-item tuple consisting of the argument name and 
the possible type.  For keyword, 3-item tuple with name, default value, and 
possible type.  Lacking *args and/or **kwargs could just be set to None for 
those tuple positions.

Since this is mainly for introspection tools the format can stand to be verbose 
and not the easiest thing to read by eye, but it must contain all possible info 
on the arguments.  And if actual execution does not use this slot, as Phillip 
is suggesting, but it is only for informative purposes we could make it 
optional.  It could also be set it to a descriptor that dynamically creates the 
tuple when called based on the function passed into the descriptor at creation 
time.  This could be rolled into the update_meta (or whatever it ends up being 
called) function.


More information about the Python-Dev mailing list