Re: Importing Version Specific Modules




Michael> if sys.version_info[:2] >= (2, 5):
Michael> from string import Template
Michael> else:
Michael> from our.compat.string import Template

This is "look before you leap" (LBYL).

Michael> try:
Michael> from string import Template
Michael> except ImportError:
Michael> from our.compat.string import Template

This is "easier to ask forgiveness than permission" (EAFP). This tends to
be more Pythonic (and thus, preferred). You don't *really* care what the
version just, but whether or not the string module has a Template object.
Besides, what if some bright admin at some customer decides to backport the
Template implementation to their 2.4 installation? The first form would
load your compatibility version, probably not what they were hoping when
they put in the effort to backport.

This might be more obvious if the example was ctypes, a module which is
available recently as part of Python proper, but can also be built for older
versions of Python (for some vague definition of "older"). Some of your
customers might have installed the external version to work with their
Python installation. Your simple version test would miss that.

--
Skip Montanaro - skip@xxxxxxxxx - http://smontanaro.dyndns.org/
.



Relevant Pages

  • Re: Eine Vorlage aus Access heraus öffnen
    ... Michael ... >> schaffe ich es aber nicht, die Tabelle unter Benutzung der Vorlage zu ... > auf dem Template erstellen. ... > Erstellt eine neue Arbeitsmappe, ...
    (microsoft.public.de.excel)
  • Re: Importing Version Specific Modules
    ... Michael> from string import Template ... Michael> from our.compat.string import Template ... what if some bright admin at some customer decides to backport the ...
    (comp.lang.python)
  • Re: Harshmans end Part one
    ... Now on any INDIVIDUAL lifeform, ... But if you wanted to make numerous programs, using one template, you ... Earlier in that thread the OP was Michael from 209.2.60.83 ... from Michael on Jan 9th from 209.2.60.95 aka PaeTec Communications, ...
    (talk.origins)
  • Re: serious word template issue in Word 2003
    ... confirm that patch KB887979 is the culprit. ... >> template's dialog box, I'm going to use that to enable a template ... >>> to see if they can provide a better way to fix this problem. ...
    (microsoft.public.office.developer.vba)
  • Re: Clarett Cut By Broncos
    ... >> Michael wrote: ... >>> The Broncos are flush with backs. ... >> he was 4th string, behind Ron Dayne, and NEVER played in presesason, ... >> nursing a little hamstring injury. ...
    (rec.sport.football.college)