[Python-ideas] Introduce collections.Reiterable

Ryan rymg19 at gmail.com
Sat Sep 21 20:15:58 CEST 2013


I can still see why checking if its callable is a good idea in some cases. Say you call the callback in line 1024 of module mymod:

self.call[item]()

And someone hands over a string:

TypeError: 'string' object is not callable

And this is what's probably going through the person's head:

Stupid Python! What did I do wrong now?

Checking if it's callable works better:
if not callable(self.call[item]):
 raise CallbackError('given callback %s must be callable' % str(item))

Now the user says:

Ohhhhh....so that's what I did wrong!!!

Ben Finney <ben+python at benfinney.id.au> wrote:

>Neil Girdhar <mistersheik at gmail.com>
>writes:
>
>> However, there are plenty of times where you can't do that, e.g., you
>> want to know if something is callable before calling it
>
>What is a concrete example of *needing* to know whether an object is
>callable? Why not just use the object *as if it is* callable, and the
>TypeError will propagate back to whoever fed you the object if it's
>not?
>
>> and similarly if something is reiterable before iterating it and
>> exhausting.
>
>I have somewhat more sympathy for this desire; duck typing doesn't work
>so well for this, because by the time the iterable is exhausted it's
>too
>late to deal with its inability to re-start.
>
>Still, though, this is the kind of division of responsibility that
>makes
>a good program: tell the user of your code (in the docstring of your
>class or function) that you require a sequence or some other
>re-iterable
>object. If you try something that fails on what object you've been
>given, that's the responsibility of the code that gave it to you. You
>can be nice by ensuring it'll fail in such a way the caller gets a
>meaningful exception.
>
>-- 
>\        “The number of UNIX installations has grown to 10, with more |
> `\         expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 |
>_o__)                                                                 
>|
>Ben Finney
>
>_______________________________________________
>Python-ideas mailing list
>Python-ideas at python.org
>https://mail.python.org/mailman/listinfo/python-ideas

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130921/b620d198/attachment.html>


More information about the Python-ideas mailing list