Status of PEP's?

James_Althoff at i2.com James_Althoff at i2.com
Thu Feb 28 18:52:38 EST 2002


[Tim Delaney]
> I never think of it though as
>
> for i in 5:
>     print i
>
> but always as
>
> for i in len(seq):
>     print i
>
> or something similar, which has obvious semantic logic.

And I think such context makes the example easy to understand.  (And, yes,
abuse is possible).

> However, I could see myself happily using for i in 5: in a quick script.

Me too.  :-)

> One important thing to remember, which I think a few people have missed,
> is that an integer will not be an iterator. An integer will be iterable -
> that is, to get this behaviour, iter(integer) must be called at some
stage.
> This is done transparently in a for: loop.

Very good point.

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

Good question.  I was wondering that, myself.  I'm guessing that the answer
is no because of the following:

>>>
>>> 1 in 'spam'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'in <string>' requires character as left operand
>>>
>>> 1 in iter('spam')
0
>>>

Jim





More information about the Python-list mailing list