[Python-3000] callable()

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Wed Jul 19 11:11:41 CEST 2006


Greg Ewing wrote:
> Guido van Rossum wrote:
> 
>> But I'm not proposing to use hasattr(obj, '__call__'). I'm basically
>> saying that the callable test has very little to do with whether the
>> later call will succeed, because we have no way to test the signature.
> 
> I don't think that's needed for the sort of things people
> want callable() for. I think they want it for the purpose
> of implementing type-dependent APIs -- similar in spirit
> to doing different things depending on whether you're
> passed a sequence or not, etc.

Exactly.


> For that purpose, hasattr(obj, '__call__') is sufficient,

But it's not the same: it's concerned with implementation details and unless
you are used to reading it, it's not obvious that both are equivalent. It
doesn't matter if it's the same. It just feels different to use it.

As I said, if it's not wanted as a builtin, there's always the way to move it
into a module. I proposed "types" for this, but I guess "inspect" is a better
place:

	inspect.iscallable(obj)

Then it's a matter of importing this function as "callable" to provide
backwards compatibility (both in terms of code and readability).

The same applies to "__iter__", BTW. What about adding these two also:

	inspect.isiterable(obj) -> hasattr(obj, "__iter__")
	inspect.isiterator(obj) -> isiterable(obj) and hasattr(obj, "next")

Same reasoning: getting rid of implementation details by using abstracted,
well-named interface tests.

Stefan


More information about the Python-3000 mailing list