Image

Imaged_m wrote in Imagepython_dev

# introspection questions behind the cut tag



Python 2.3.4 (#1, Oct 19 2004, 09:10:52)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> # ok, this goes out to all the python kids that are crazy about
... # introspection.
... def foo(a,b,c=1,d=2):
...     return a*b*c*d
...
>>> # these are all the elements and methods associated with the function:
... dir(foo)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
'func_doc', 'func_globals', 'func_name']
>>> 
>>> # this is where the default values for c and d are stored:
... foo.func_defaults
(1, 2)
>>> 
>>> # this is where the function's name is stored:
... foo.__name__
'foo'
>>> 
>>> # this is the code object that gets executed when you use __call__:
... foo.func_code
<code object foo at 0xb7d6d7e0, file "<stdin>", line 1>
>>> 
>>> # but my question is, is there anything that gives you a list of
... # the expected arguments (and their names): e.g. something that
... # would return ['a', 'b', 'c', 'd']??
... #
... # python can obviously do it, since you get these errors:
...  foo(1)
Traceback (most recent call last):
  File ">stdin<", line 1, in ?
TypeError: foo() takes at least 2 arguments (1 given)
>>> 
>>> foo(1,2,3,4,5)
Traceback (most recent call last):
  File ">stdin<", line 1, in ?
TypeError: foo() takes at most 4 arguments (5 given)
>>> 
>>> # i have been scouring things but can't find any answers.
... #
... # thanks in advance!
...
>>>