On 11/19/06, Greg Ewing <[EMAIL PROTECTED]> wrote:
> Andrew Koenig wrote:
>
> > That's a nice general sentiment, but not always feasible -- especially if
> > you're writing library code.
>
> I've written a fair bit of library code, and I've never
> found a pressing need to test for an abstract interface.
>
> In the rare cases where I've used type testing in an
> API, it's always been for specific well-known concrete
> types, such as strings and tuples -- never anything so
> abstract as "iterator", "sequence", etc.

Sometimes people don't realize how much they'd use a feature until it
exists.  I never thought I'd use generators until I started using
them, and then it's "Wow, this is neat!"  There have been several
cases like this with Python.

Another way people often test for sequence-ness:
    isinstance(obj, (list, tuple))
    isinstance(obj, (list, tuple, UserList))
Here one buldes up all the likely classes.  That's fine but we left
out str because you have to stop somewhere.  Usually I forego UserList
because that depends on a module that may not be used, and often I
forego tuple to make it shorter: "isinstance(obj, list)".  That's fine
but it's more restrictive than I want to me: my function *could* work
with a tuple or list-like object too.

Equality for tuples!  Down with sequence discrimination!  Support interfaces! :)

-- 
Mike Orr <[EMAIL PROTECTED]>
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to