runtime-info about a function-object

Andreas Leitgeb Andreas.Leitgeb at siemens.at
Mon Aug 19 18:04:47 CEST 2002


>>> def f(x,y,z=someobj,*arg,**dict): pass
...
>>> f
<function f at 0x8167bb4>

Is it possible to request info about function-object "f",
 especially about the number/name/defaults of arguments
 that f accepts ?

something like: (invented builtin)
>>> signature(f)
(('x',),('y',),('z',<someClass instance at 0x...>),('*arg',),('**dict',))
or just:
(2,3,true,true)    
quadruple meaning: 
  min # of args=2;   # of named args;   takes *arg;   takes **dict.
or whatever is internally available and parse-able at run-time.


The point is: even if a function lacks an informative __doc__-string, 
the list of named parameters (the signature) usually gives me some extra 
information about the function, even if all I see is the function-object 
lying around in a "python -i ..."-session.

Also, it might be useful, if one likes to do some sanity-checking 
beyond the simple callable()-test, but not actually test-calling
the object in a try-block beforehand.

It may also be nice to know, if a function is actually a generator.
Thus, for g being a generator,
  __repr__(g) might return: "<generator g at 0x...>"




More information about the Python-list mailing list