Callable assertion?

Gerrit Holl gerrit at nl.linux.org
Mon Oct 6 05:52:12 EDT 2003


Gonçalo Rodrigues wrote:
> On Sun, 05 Oct 2003 10:20:11 -0400, Roy Smith <roy at panix.com> wrote:
> > "A.M. Kuchling" <amk at amk.ca> wrote:
> >> There's a callable(param) built-in function, dating back to Python 1.2.

> >What does "appears callable" mean?  Under what circumstances would 
> >callable(foo) return True, yet foo() would fail?
> 
> An extreme and artificial example:
> 
> >>> class FakeCallable(object):
> ... 	def __call__(self, *args, **kwargs):
> ... 		raise TypeError("My only purpose in life is to trick
> the Python interpreter.")
> ... 
> >>> a = FakeCallable()
> >>> callable(a)
> True
> >>> a()
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "<interactive input>", line 3, in __call__
> TypeError: My only purpose in life is to trick the Python interpreter.

Is there a difference between callable(a) and hasattr(a, '__call__')?

Gerrit.

-- 
121. If any one store corn in another man's house he shall pay him
storage at the rate of one gur for every five ka of corn per year.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
	http://www.sp.nl/





More information about the Python-list mailing list