En Sat, 31 Jan 2009 18:22:54 -0200, Gary Herron
<gher...@islandtraining.com> escribió:
James Pruitt wrote:
Imagine there are two files horse.py and buffalo.py. horse.py is
imported by another file rider.py. Is it possible to make it so that
under certain circumstances possibly based on an environment variable
or something similar that when rider.py imports horse.py, it actually
imports buffalo.py sort of like a behind the scenes replacement so
that rider.py needs little, preferably absolutely no modification?
If horse and buffalo have the same interface then try something like
this:
if ...:
import horse as ridable
else:
import buffalo as ridable
# Now use ridable as any module...
If each defines a class of its own name, but the classes have identical
interfaces, then try
if ...:
from horse import Horse as Ridable
else:
from buffalo import Buffalo as Ridable
# Then instantiate
animal = Ridable(...)
Another alternative, that does not involve changing rider.py, would be to
rename horse.py -> _horse.py, buffalo.py -> _buffalo.py and write a *new*
horse.py:
if ...:
from _horse import *
else:
from _buffalo import *
Then, rider.py (and all other modules) still says "import horse", but it
will get one or another depending on the condition.
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list