Multi-isinstance idiom (Re: Deprecate tabs for indenting (was Re: Indenting with tabs vs spaces))

Mike C. Fletcher mcfletch at rogers.com
Thu Dec 6 07:58:08 CET 2001


That doesn't seem to keep the semantics, it only works if the tested 
class is an actual sub-class of your combination class, not if it's a 
child of just one of the parents (that is, it's an "and" test with an 
extra class added as well).  If you want an or test,  how about:

try:
    raise f
except (future.Step, yada, yada):
    blah
except:
    stuffWhenNotSubClass

Problem is that it's nothing like intuitive, an explicit function like:

def ismultiinstance( test, classes ):
    for classObject in classes:
        if isinstance( test, classObject):
            return 1
    return 0

Would be much easier to read, though not very computationally efficient 
(extra function call, loop overhead) compared to letting the C exception 
machinery handle the project.

Enjoy,
Mike


Greg Ewing wrote:

>Bjorn Pettersen wrote:
>
>>>From: Courageous [mailto:jkraska at san.rr.com]
>>>
>>>    if isinstance(f, future.Step)          or \
>>>       isinstance(f, future.Status)        or \
>>>       isinstance(f, future.Announce)      or \
>>>       isinstance(f, possibility.Observe)  or \
>>>       isinstance(f, future.Incarnate)     or \
>>>       isinstance(f, future.Start)         or \
>>>       isinstance(f, future.Timeout):
>>>
>>Ack, so much typing :-)
>>
>
>How about
>
>  class ClassesOfInterest(future.Step, future.Status, future.Announce,
>    possibility.Observe, future.Incarnate, future.Start,
>future.Timeout):
>      pass
>
>  if isinstance(f, ClassesOfInterest):
>    ...
>






More information about the Python-list mailing list