callable to disappear?

A

Antoon Pardon

I have been reading http://www.python.org/dev/peps/pep-3100/
en there is written:

To be removed:
...

callable(): just call the object and catch the exception

...

But that doesn't seem to be a generally available option.
The place where you want to check if something is callable
doens't need to be the place where you actually want to call
it. Removing callable will mean that you can't check whether
or not something is callable without incurring the side-effects
of calling it.

I also think code will become more ugly

How do you suggest I would code the following:

if callable(func):
for i, el in lst:
lst = func(el)
othercode()


I can code as follows:

try:
for i, el in lst:
lst = func(el)
othercode()
except TypeError:
pass


But this has the problem that othercode could throw a TypeError:

So it seems I would need at least two try statements

try:
for i, el in lst:
try:
lst = func(el)
except TypeError
raise LoopBreak
othercode()
except LoopBreak:
pass

And this still has problems because the TypeError could be
raised because lst is an unsubscriptable object.


Is there a chance this will be reconsidered?
 
T

Terry Reedy

faulkner said:
what's wrong with hasattr(obj, '__call__')?

I have the impression that this is not currently true for all callables .
If not, this may be improved in the future.

The items in PEP 3100 have different levels of certainly. Some may even
get changed after experience with the alpha versions. Guido is allowing a
year from first alpha (early 2007?) to final release, instead of the
current 4-5 months.

There has been recent discussion since of iscallable and some other
isxxxx()s. The points you mentioned were raised and considered. I don't
remember if there was a decision for the present.

Two of the bigger negatives for 'iscallable':
1. You cannot know for sure until you call and get a return.
2. It does not say if the candidate is callable with any particular number
or set of parameters.

Terry Jan Reedy
 
F

Fredrik Lundh

Terry said:
1. You cannot know for sure until you call and get a return.

with that argument, you might as well remove functions and methods from
the language. after all, anything can happen when you call a function.

not to mention what import can do.

scary.

</F>
 
A

Antoon Pardon

I have the impression that this is not currently true for all callables .
If not, this may be improved in the future.


The items in PEP 3100 have different levels of certainly. Some may even
get changed after experience with the alpha versions. Guido is allowing a
year from first alpha (early 2007?) to final release, instead of the
current 4-5 months.

There has been recent discussion since of iscallable and some other
isxxxx()s. The points you mentioned were raised and considered. I don't
remember if there was a decision for the present.

Two of the bigger negatives for 'iscallable':
1. You cannot know for sure until you call and get a return.

Even that is not sure. Consider the following:

Bar = 1

def Foo():
Bar()

try:
Foo()
except ...:
warning("...")


Calling Foo will result in the same exception being raised as if
Foo wasn't callable, while in fact it is callable.
2. It does not say if the candidate is callable with any particular number
or set of parameters.

But the "raise an exception" option doesn't make this distinction either.
A TypeError will be raised both when the object isn't callable or when
it is called with the wrong parameters.
 
G

Georg Brandl

Antoon said:
I have been reading http://www.python.org/dev/peps/pep-3100/
en there is written:

To be removed:
...

callable(): just call the object and catch the exception

...

Is there a chance this will be reconsidered?

There was some discussion on python-dev, which concluded that callable()
in its current form is not very useful, since even when you know that
something's callable, the call might fail as well because the callable
doesn't have the correct signature.

So, it's not impossible for Py3k to get an improved version of callable()
including signature checking.

Georg
 
B

Bruno Desthuilliers

Georg Brandl a écrit :
There was some discussion on python-dev, which concluded that callable()
in its current form is not very useful,

I use it quite frequently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,274
Latest member
JessMcMast

Latest Threads

Top