callable to disappear?

Discussion in 'Python' started by Antoon Pardon, Aug 23, 2006.

  1. 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?

    --
    Antoon Pardon
     
    Antoon Pardon, Aug 23, 2006
    #1
    1. Advertising

  2. Antoon Pardon

    faulkner Guest

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

    Antoon Pardon wrote:
    > 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?
    >
    > --
    > Antoon Pardon
     
    faulkner, Aug 23, 2006
    #2
    1. Advertising

  3. Antoon Pardon

    Terry Reedy Guest

    "faulkner" <> wrote in message
    news:...
    > 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.

    >> Antoon Pardon
    >> Is there a chance this will be reconsidered?


    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
     
    Terry Reedy, Aug 23, 2006
    #3
  4. Terry Reedy wrote:

    > 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>
     
    Fredrik Lundh, Aug 24, 2006
    #4
  5. On 2006-08-23, Terry Reedy <> wrote:
    >
    > "faulkner" <> wrote in message
    > news:...
    >> 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.
    >
    >>> Antoon Pardon
    >>> Is there a chance this will be reconsidered?

    >
    > 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.

    --
    Antoon Pardon
     
    Antoon Pardon, Aug 25, 2006
    #5
  6. Antoon Pardon

    Georg Brandl Guest

    Antoon Pardon wrote:
    > 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
     
    Georg Brandl, Aug 25, 2006
    #6
  7. Georg Brandl a écrit :
    > Antoon Pardon wrote:
    >
    >> 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,


    I use it quite frequently.
     
    Bruno Desthuilliers, Aug 27, 2006
    #7
  8. Antoon Pardon

    Georg Brandl Guest

    Bruno Desthuilliers wrote:
    > Georg Brandl a écrit :
    >> Antoon Pardon wrote:
    >>
    >>> 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,

    >
    > I use it quite frequently.


    How nice.

    Georg
     
    Georg Brandl, Aug 28, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Gilbert
    Replies:
    3
    Views:
    2,333
    Gordon Beaton
    Apr 14, 2004
  2. Erik Max Francis

    Re: 'int' object is not callable

    Erik Max Francis, Jul 23, 2003, in forum: Python
    Replies:
    0
    Views:
    1,653
    Erik Max Francis
    Jul 23, 2003
  3. Roy Smith

    Callable assertion?

    Roy Smith, Oct 5, 2003, in forum: Python
    Replies:
    23
    Views:
    626
    Alex Martelli
    Oct 8, 2003
  4. exiquio
    Replies:
    2
    Views:
    565
    exiquio
    Oct 7, 2008
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    141
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page