
Signature.py is a module that makes reflecting on the function call signatures of callable objects a lot easier. A common question in Python is "How do I ask the interpreter what arguments a function takes?" and while the information is obviously available, I haven't found any simple, high-level interfaces to it. That's why I wrote Signature -- it's useful enough that it's made it into my personal library of frequently-used utilities. You can find it at: http://www.sff.net/people/neelk/free-software/Signature.py Caveats/TODO: o I don't know if it will work on JPython -- I exploited a lot of undocumented object attributes to figure out what the call signature is, and I have no idea if the same data is available there. o I haven't figured out how to look into C builtins yet -- anyone who can teach me how to do this from within Python will earn my undying gratitude. :) Example of use: >>> def foo(x, y, z=-1.0, *args, **kw): ... return (x+y)**z ... >>> f = Signature(foo) >>> print 'ordinary arglist:', f.ordinary_args() ordinary arglist: ('x', 'y', 'z') >>> print 'special_args:', f.special_args() special_args: {'keyword': 'kw', 'positional': 'args'} >>> print 'full arglist:', f.full_arglist() full arglist: ['x', 'y', 'z', 'args', 'kw'] >>> print 'defaults:', f.defaults() defaults: {'z': -1.0} >>> print 'signature:', str(f) signature: foo(x, y, z=-1.0, *args, **kw) Neel <P><A HREF="http://www.sff.net/people/neelk/free-software/Signature.py"> Signature 0.1</A> - for reflecting on the function call signatures of callable objects. (09-Jul-99) -- ----------- comp.lang.python.announce (moderated) ---------- Article Submission Address: python-announce@python.org Python Language Home Page: http://www.python.org/ Python Quick Help Index: http://www.python.org/Help.html ------------------------------------------------------------
participants (1)
-
neelk@neelk