[Python-3000] Builtin iterator type

George Sakkis george.sakkis at gmail.com
Wed Nov 15 21:34:24 CET 2006


On 11/15/06, Jim Jewett <jimjjewett at gmail.com> wrote:

> (list clipped)

Sorry, I'd rather not get into private conversations.

> On 11/15/06, George Sakkis <george.sakkis at gmail.com> wrote:
> > Note that this does _not_ disallow duck typing; anyone is perfectly
> > free to define a method len() in a class that does not extend any of
> > the above so that a client can call x.len() successfully.
>
>
> Not if
>     >>> if instance(x, Sequence):
>
> becomes a standard idiom

It doesn't have to, and it shouldn't, become the standard; after all,
whatever you do in the "else:" part of the isinstance check, you can
do in a try/except block. This has nothing to do with type checking
per se, it's a general LBYL vs EAFP error handling strategy. Python in
general encourages the latter and I totally agree, there's no reason
for this to change. In the few cases where LBYL is deemed better and
you'd use isinstance, you can do the same today with
    if hasattr(x, '__getitem__'):
or whatever attributes you need.

George


More information about the Python-3000 mailing list