Backwards compatibility [was Re: is parameter an iterable?]

Fredrik Lundh fredrik at pythonware.com
Wed Nov 23 16:10:18 EST 2005


Tom Anderson wrote:

> How about detecting which environment you're in, then running one of two
> entirely different sets of code? Rather than trying to construct modern
> features in the antique environment, write code for each, using the local
> idioms. The trouble with this is that you end up with massive duplication;
> you can try to factor out the common parts, but i suspect that the
> differing parts will be a very large fraction of the codebase.

That sounds like the worst possible way of writing portable code (unless you
have huge amounts of time and money, and wasting them don't really matter
to you).

In my experience, a more practical approach is to write code that targets the
2.0/2.1 platform, and selectively (based on feature tests, not version numbers)
replace portions with more efficient mechanisms from newer versions.  (most
of those are found in the standard library, not in the language itself).

(this is related to another popular myth: that code that uses "the old way" is
automatically slower than code that uses newfangled stuff, also when running
on recent Python versions.  that's very seldom true; new Python versions runs
"old" code faster too.)

If you want to support 1.5.2 or Unicode-less Python builds, you need to add
some fallbacks too, but that's mostly trivial.  The easiest way is to pass 8-bit
strings through "as is", leaving encoding issues to the application.

>> If I have to write code that can't rely on iter() existing in the
>> language, what should I do?
>
> Can you implement your own iter()? I have no idea what python 2.0 was
> like, but would something like this work:

Python 2.0 had sequences.  Python 2.4 has sequences.  Iterators don't really
add anything.  Emulating them in older versions is mostly pointless.

I've said it many times, and I'll say it again: the only fundamentally new concept
that has been added since Python 1.5.2 is generators.  And a lot of the stuff you
want generators for can be emulated with sequences and extra buffer layers.

All the rest is just coloured frosting; you can save a line here and there (which is
a real benefit only if your return key is broken), but that's about it.

</F> 






More information about the Python-list mailing list