[Python-ideas] Allow isinstance second argument to be a set of types

Ryan Kelly ryan at rfk.id.au
Fri Jul 8 00:28:11 CEST 2011


On Thu, 2011-07-07 at 23:12 +0100, Michael Foord wrote:
> 
> 
> On 7 July 2011 20:12, Antoine Pitrou <solipsis at pitrou.net> wrote:
>         On Thu, 7 Jul 2011 15:08:54 -0400
>         Mike Graham <mikegraham at gmail.com> wrote:
>         > On Thu, Jul 7, 2011 at 1:43 PM, Antoine Pitrou
>         <solipsis-xNDA5Wrcr86sTnJN9+BGXg at public.gmane.org> wrote:
>         > > On Thu, 7 Jul 2011 13:22:33 -0400
>         > > Jim Jewett <jimjjewett at gmail.com> wrote:
>         > >> On Wed, Jul 6, 2011 at 7:35 PM, Benjamin Peterson
>         <benjamin-+ZN9ApsXKcEdnm+yROfE0A at public.gmane.org> wrote:
>         > >>
>         > >> > class Evil(type):
>         > >> >    def __iter__(self):
>         > >> >        return range(42)
>         > >>
>         > >> > class Accomplice(metaclass=Evil):
>         > >> >    pass
>         > >>
>         > >> ... leading to isinstance(x, 42) rather than
>         isinstance(x, Evil) if
>         > >> the iterable check is done too soon.
>         > >
>         > > In what way is that a problem?
>         > > That you can trigger exceptions by writing malicious code
>         doesn't sound
>         > > like a particularly novel event ;)
>         >
>         > I think people are assuming that an iterable metaclass isn't
>         > fundamentally wrong.
>         
>         
>         Well, it does sound fundamentally useless!
> 
> I've used indexable types before. I've never created an iterable one,
> but it isn't unthinkable. :-)

I've created one, once.  It was a metaclass for defining enumerations
like this:

    class COLOURS(Enum):
        RED = 1
        BLUE = 2
        GREEN = 3

If you wanted to iterate over all the members of the enum, you could do:

    for colour in COLOURS:
        blah(colour)


Of course, in this case it would be totally useless to pass COLOURS into
isintance().  But I don't see what doing so should be capable of
breaking things.



   Ryan


-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
ryan at rfk.id.au        |  http://www.rfk.id.au/ramblings/gpg/ for details

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110708/b96915ae/attachment.pgp>


More information about the Python-ideas mailing list