Iterating over multiple lists (a newbie question)

Moshe Zadka moshez at zadka.site.co.il
Sun Jan 7 12:28:46 EST 2001


[Alex Martelli]
> ...
> The map builtin function, OTOH, does need to know the length of
> each sequence in advance (and the _longest_ one controls, with
> the others being 'extended' with as many 'None' as needed), so,
> no such techniques (tricks?-) are available in that case.

[Tim Peters]
> Actually, they are.  map is controlled by IndexError, same as everything
> else.  It's a rather muddy historical tale as to why it insists that len(s)
> be defined for each sequence argument s, but the largest of those is used
> only as "a hint", to preallocate a result list.  If the last occurrence of
> IndexError occurs before or after that length is reached, map shrinks or
> grows the result list accordingly.
<snipped demo>

So why can't map just assume things without length have length 0?

>From my reading of the source, you'd have to change the 

                if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
                        goto Fail_2;

Into a special case for instances which first checks to see whether __len__
exists, and take the sqf->sq_length check out of the if.
Is there something bad about doing this, or has it just been lack of time?


-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!




More information about the Python-list mailing list