is parameter an iterable?

Fredrik Lundh fredrik at pythonware.com
Mon Nov 21 09:01:05 EST 2005


Steven D'Aprano wrote:

> Alas and alack, I have to write code which is backwards
> compatible with older versions of Python:
>
> Python 2.1.1 (#1, Aug 25 2001, 04:19:08)
> [GCC 3.0.1] on sunos5
> Type "copyright", "credits" or "license" for more
> information.
>  >>> iter
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> NameError: name 'iter' is not defined
>
> What should I do when I can't rely on functions that
> don't exist in older versions of Python?

python 2.1 doesn't support iterators, so that question doesn't
make much sense.

$ python2.1

>>> class iterable:
...     def __iter__(self):
...             print "ITER"
...             return self
...     def next(self):
...             print "NEXT"
...             return 1
...
>>> for i in iterable():
...     print i
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: iterable instance has no attribute '__getitem__'

if you want to write code that runs under 2.1, you have to write
your code in terms of what 2.1 supports.   Python's compatibility
model means that code written for old versions still work in new
versions; it doesn't mean that code written for new versions will
always work properly (or raise proper exceptions) in old versions.

</F>






More information about the Python-list mailing list