How do I say "Is this a function"?

alex.gaynor at gmail.com alex.gaynor at gmail.com
Sat Apr 26 21:01:47 EDT 2008


callable(func) returns whether something is callable(will return true
for classes, functions, and objects with __call__ methods).

On Apr 26, 6:25 pm, Dan Bishop <danb... at yahoo.com> wrote:
> On Apr 26, 6:17 pm, John Henry <john106he... at hotmail.com> wrote:
>
>
>
> > How do I determine is something a function?
>
> > For instance, I don't want to relying on exceptions below:
>
> > def f1():
> >    print "In f1"
>
> > def f3():
> >    print "In f3"
>
> > def others():
> >    print "In others"
>
> > for i in xrange(1,3):
> >    fct = "f%d()"%(i+1)
> >    try:
> >       exec fct
> >    except:
> >       others()
>
> > I wish to say:
>
> >    if value of fct is a funtion, invoke it, otherwise invoke others().
>
> > Thanks,
>
> hasattr(fct, '__call__')
>
> And be careful about using the exec statement.




More information about the Python-list mailing list