Why is there no instancemethod builtin?

John Reese jtr at ofb.net
Fri Jun 17 19:59:04 EDT 2005


On Fri, 17 Jun 2005 16:40:56 -0600, <steven.bethard at gmail.com> wrote:
> John Reese wrote:
>> I now do:
>>  
>>   if isinstance(x, list):
>>  
>> It is my understanding that this is what people do nowadays.
>
> I wouldn't go that far.  I don't have an isinstance check for lists 
> anywhere in my entire codebase.  Why do you think you need to check to 
> see if something is of type list?  Why don't you just use it as needed, 
> and find out, e.g.:
>
> try:
>      itr = iter(x)
> except TypeError:
>      # do whatever you need to do if it's not iterable
> else:
>      # do whatever you need to do if it *is* iterable
>
> STeVe

I'm not saying I do it a lot, but sometimes it's useful to write
methods with interfaces like, well, isinstance's, whose second argument
can be a single type object or a sequence of class objects.  The
standard conditional vs. exception tradeoffs exist here... if it's
likely that x isn't iterable, I shouldn't have to incur the time and
heap churn penalty of filling in sys.exc_info and its obsolete
cousins, potentially rolling back the stack, etc.



More information about the Python-list mailing list