[Tutor] Function type?

Abel Daniel abli@freemail.hu
Tue Jun 10 15:05:29 2003


Arntson wrote:
> Okay, so I can use isinstance as follows:
> 
> >>> isinstance('a', str)
> 1
> >>> isinstance(23, int)
> 1
> 
> But what's the shortcut keyword for a function type? Right now, I have to
> use a dummy function:
> 
> >>> def a(): pass
> >>> def squared(x): print x**2
> >>> isinstance(b, type(a))
> 1
> 
> Is there a keyword I'm being oblivious to? Or do I need to resort to this
> dummy function thing?
You need callable().
Note that most of the time you shouldn't need to do this. Usually people
want to check types to ensure they got the right data. However, trying
to do whatever you want to do with it, and letting the user take care of
the exceptions raised is usually cleaner. (Where user == "the programmer
who uses the code you wrote".)

Abel Daniel