Status of PEP's?

Tim Peters tim.one at comcast.net
Thu Feb 28 18:54:46 EST 2002


[Delaney, Timothy]
> ...
> Are the following two equivalent though?

Not necessarily, but the possible difference isn't where you think:

> if 1 in obj:
>     pass
>
> if 1 in iter(obj):
>     pass
>
> i.e. is iter(obj) called implicitly in the first case?

Maybe.  First type(obj) is checked to see whether it implements __contains__
(at the C level, whether obj->ob_type->tp_as_sequence->sq_contains is
reachable and non-NULL).  If it is, iter() gets involved if and only if the
__contains__ method decides to use iter() (and, for example, dicts certainly
do not).  If type(obj) doesn't implement __contains__, then "in" tickles
iter(obj) repeatedly.





More information about the Python-list mailing list