other ways to check for <type 'function'>?

elderic elderic at ish.de
Thu Nov 2 15:40:31 CET 2006

> What's the problem with this?
> from types import FunctionType
> if isinstance(f, FunctionType):
>     ...
> Bye,
> bearophile

Well... it's discouraged by the docs =)

At least the use of module types.
I was just wondering if there were some alternatives.
Never thought I would start off a thread this long =)

That was basically my reason for asking about something similar like
the functions list(), dict(), int(), etc... when called without (),
they return <type 'sometype'>.
I just wanted to know if there was a keyword for functions, too.

Then u could've done: type(f) is function
quite similar to: type(x) is int

Didn't intent to be a Documentation-Nazi *G*


Excuse my large paste from the Python 2.5 Documentation:
5.15 types -- Names for built-in types


Typical use is for functions that do different things depending on
their argument types, like the following:

from types import *
def delete(mylist, item):
    if type(item) is IntType:
       del mylist[item]

Starting in Python 2.2, built-in factory functions such as int() and
str() are also names for the corresponding types. This is now the
preferred way to access the type instead of using the types module.
Accordingly, the example above should be written as follows:

def delete(mylist, item):
    if isinstance(item, int):
       del mylist[item]

More information about the Python-list mailing list