How to access a function's formal signature?

K

kj

In the standard Python interactive interpreter, the string printed
by the help command when applied to a function includes the function's
formal signature. E.g.:
.... pass
.... Help on function foo in module __main__:

foo(bar, *baz, **frobozz)


Here by "signature" I'm referring to the substring "bar, *baz,
**frobozz" shown above (or, equivalently, any other object from
which this string could be deduced). (I figured out that I can
retrieve this signature using methods in the inspect module.)

OK, now, is there a way to modify a function so that pydoc (and
presumably also the interactive interpreter's help function, etc.)
will print out a desired specific signature for this function?

For example, is there some way that I can modify foo above so that
help(foo) will print out

foo(x, y=None)

I tried mucking with foo.func_code's co_argcount, co_varnames, and
co_flags attributes, but as it turns out, they are read-only,
unfortunately.

The context here is the problem of writing a signature-changing
decorator in such a way that documentation facilities like help
and pydoc will print out the signature of the *original* undecorated
code (with some modifications), rather than the signature of the
decorated method.

TIA!

kynn
 
C

Carl Banks

In the standard Python interactive interpreter, the string printed
by the help command when applied to a function includes the function's
formal signature.  E.g.:


...   pass
...>>> help(foo)

Help on function foo in module __main__:

foo(bar, *baz, **frobozz)

Here by "signature" I'm referring to the substring "bar, *baz,
**frobozz" shown above (or, equivalently, any other object from
which this string could be deduced).  (I figured out that I can
retrieve this signature using methods in the inspect module.)

OK, now, is there a way to modify a function so that pydoc (and
presumably also the interactive interpreter's help function, etc.)
will print out a desired specific signature for this function?

I think you'd have to modify pydoc. pydoc pulls this information out
from the function's attributes (and maybe also it's underlying code
object) which are read-only.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top