[Tutor] what instance type is a function object?

Kent Johnson kent37 at tds.net
Thu Feb 22 23:09:44 CET 2007


Adam Pridgen wrote:
> Thanks in advance for your help.
> I am trying to pickle a class, but I want to exclude function objects
> for obvious reasons.  How do I determine if a python object is a
> function, specifically when using isinstance()?  I am not sure what a
> type or instance a Python 'function' originates from.

You can either use type(f) where f is some function or import types and 
use types.FunctionType.

You could also use the builtin function callable() but that will be true 
for any callable (e.g. an instance of a class that has a __call__() 
method) not just for functions.

Kent

> 
> The below code throws an exception, because function is not a type.
> Presently I am attempting to do the following:
> 
> def __getstate__(
>    ret_val = {}
>    for key in self.__dict__:
>       if not isinstance(self.__dict__[key], function): pass
>       # do assignment here
> 
> Thanks again, Adam
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list