PEP 276 (was Re: Status of PEP's?)
Bernhard Herzog
bh at intevation.de
Fri Mar 1 09:37:45 EST 2002
James_Althoff at i2.com writes:
> [Aahz Maruch]
> > My primary complaint about the PEP is that the following idiom is
> > awkward, ugly, and prone to misunderstanding:
> >
> > if i in 5:
>
> I will note this.
>
> However given that current Python seems able to do type-checking on strings
> used with "in" as in, for example:
>
> >>> 1 in 'spam'
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: 'in <string>' requires character as left operand
> >>>
That's because strings implement the in operator. However, Python is
smart enough to make in work for iterable objects. Quick experiment
(Python 2.2):
>>> from __future__ import generators
>>> class MyInt(int):
... def __iter__(self):
... for i in range(self):
... yield i
...
>>> num = MyInt(3)
>>> for i in num: print i
...
0
1
2
>>> 1 in num
1
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://mapit.de/
More information about the Python-list
mailing list