problem : test a type

polux polux2001 at wanadoo.fr
Sun Aug 25 20:34:12 EDT 2002


Hans Nowak wrote:
> polux wrote:
> 
>>
>> I'd like to test if an item in dir(math) is a function, but it doesn't 
>> work :
>>
>>
>>
>>
>>  >>> import math
>>  >>> math.cos
>> <built-in function cos>
>>  >>> math.cos==<built-in function cos>
>> SyntaxError: invalid syntax
>>  >>> math.cos=='built-in function cos'
>> 0
>>  >>>
>>
>> I've tried with "type", it doens't work better
>> the only way I found to do this is to use srt(math.item) and then test
>>
>> Can I do it differently ?
> 
> 
> You could use isinstance:
> 
>  >>> import types
>  >>> def f(x): return 2*x
> 
>  >>> isinstance(f, types.FunctionType)
> 1
> 
> ...but you're probably better off using the built-in callable() 
> function, that matches not just functions, but also methods and other 
> callable objects:
> 
>  >>> callable(f)
> 1
> 
> HTH,
> 

Thanks !
just what I needed




More information about the Python-list mailing list