[Python-3000] Builtin iterator type
sluggoster at gmail.com
Mon Nov 20 02:04:41 CET 2006
On 11/19/06, Greg Ewing <greg.ewing at canterbury.ac.nz> 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 <sluggoster at gmail.com>
More information about the Python-3000