other ways to check for <type 'function'>?
wittempj@hotmail.com
martin.witte at gmail.com
Thu Nov 2 06:28:16 EST 2006
Christophe wrote:
> Sybren Stuvel a écrit :
> > elderic enlightened us with:
> >> are there other ways than the ones below to check for <type
> >> 'function'> in a python script?
> >
> > First of all, why would you want to? If you want to call the object
> > as a function, just do so. Handle the exception that is raised when
> > it's raised.
>
> I don't think it's a good idea because when you place a try catch block
> around a function call, you'll catch any exception thrown by the
> function itself and not only the "cannot be called" exception.
A little experimentation shows that when something is not callable
always a TypeError which evaluates to "'<type>' object is not
callable", so you can refine the try/catch with this information
py> x = 1
py> x()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
py> x = ''
py> x()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable
py> class x:
... pass
...
py> x()
<__main__.x instance at 0x1002eef38>
py> def x():
py. pass
py.
py> x()
More information about the Python-list
mailing list