<br><br><div class="gmail_quote">On 7 July 2011 01:02, Alexander Belopolsky <span dir="ltr"><<a href="mailto:alexander.belopolsky@gmail.com">alexander.belopolsky@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On Wed, Jul 6, 2011 at 7:35 PM, Benjamin Peterson <<a href="mailto:benjamin@python.org">benjamin@python.org</a>> wrote:<br>
..<br>
<div class="im">> Mm. I don't blame you. I'll speak code:<br>
><br>
> class Evil(type):<br>
>    def __iter__(self):<br>
>        return range(42)<br>
><br>
><br>
> class Accomplice(metaclass=Evil):<br>
>    pass<br>
><br>
> isinstance(12, Accomplice()) # boom if arbitrary iterables are allowed<br>
<br>
</div>What is "boom"?  If "boom" is a TypeError - it is to be expected.  For<br>
example, with<br></blockquote><div><br>I think the instantiation of Accompalice there is a typo. The point is that as Accompalice (the type) is an instance of Evil and is therefore an iterable type. So if isintance naively iterated over *anything iterable* it would attempt to iterate over Accomplice. <br>
 </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
def better_isinstance(object, classinfo):<br>
    if isinstance(classinfo, (type, tuple)):<br>
        return isinstance(object, classinfo)<br>
    else:<br>
        return any(isinstance(object, cls) for cls in classinfo)<br>
<br></blockquote><div><br><br>You avoid this problem by checking explicitly for type.<br><br>Michael<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

I get:<br>
<br>
>>> better_isinstance(12, Accomplice)<br>
False<br>
<br>
and<br>
<br>
>>> better_isinstance(12, Accomplice())<br>
<div class="im">Traceback (most recent call last):<br>
</div> ..<br>
TypeError: 'Accomplice' object is not iterable<br>
<div><div></div><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
<br>