Callable assertion?
Gonçalo Rodrigues
op73418 at mail.telepac.pt
Sun Oct 5 13:44:53 EDT 2003
On Sun, 05 Oct 2003 10:20:11 -0400, Roy Smith <roy at panix.com> wrote:
>In article <wMOdnYxfQN0hux2iRTvUqg at speakeasy.net>,
> "A.M. Kuchling" <amk at amk.ca> wrote:
>
>> On Sun, 05 Oct 2003 08:55:45 -0400,
>> Roy Smith <roy at panix.com> wrote:
>> > the right thing to do, or is there something cleaner? Will that work
>> > for all callable values of param, regardless if it's a static function,
>> > class method, built-in, etc?
>>
>> There's a callable(param) built-in function, dating back to Python 1.2.
>>
>> --amk
>
>Ah. That's exactly what I was looking for! The docs say:
>
>"Return true if the object argument appears callable, false if not. If
>this returns true, it is still possible that a call fails, but if it is
>false, calling object will never succeed."
>
>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.
>>>
Best,
G. Rodrigues
More information about the Python-list
mailing list