problem : test a type
Hans Nowak
wurmy at earthlink.net
Sun Aug 25 20:24:02 EDT 2002
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,
--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.angelfire.com/jazz/aquila/blog/blogger.html
More information about the Python-list
mailing list