introspection and functions

Wildemar Wildenburger wildemar at freakmail.de
Thu Aug 23 20:18:57 EDT 2007


Ricardo Aráoz wrote:
> Ayaz Ahmed Khan wrote:
>   
>> "James Stroud" typed:
>>     
>>> py> def doit(a, b, c, x=14):
>>> ...   pass
>>> ...
>>> py> doit.func_code.co_argcount
>>> 4
>>> py> doit.func_code.co_varnames
>>> ('a', 'b', 'c', 'x')
>>> py> doit.func_defaults
>>> (14,)
>>>       
>> Neat.
>>     
> How do you know the 14 corresponds to x ?
>   
Well, there is one optional argument (len(doit.func_defaults)==1) and 
those *must* come after mendatory arguments. More generally: If a 
function has n optional args, they're the n last ones. There is never 
any ambiguity.

/W



More information about the Python-list mailing list