Adapting code to multiple platforms

Patrick Useldinger pu.news.001 at gmail.com
Sat Mar 12 08:23:42 EST 2005


Jeffrey Barish wrote:

> I have a small program that I would like to run on multiple platforms
> (at least linux and windows).  My program calls helper programs that
> are different depending on the platform.  I think I figured out a way
> to structure my program, but I'm wondering whether my solution is good
> Python programming practice.
> 

I use something like this in the setup code:

if os.name == 'posix':
   statfunction = os.lstat
else:
   statfunction = os.stat

and then further in the code:

x = statfunction(filename)

So the idea is to have your "own" function names and assign the 
os-specific functions one and for all in the beginning. Afterwards, your 
code only uses your own function names and, as long as they behave in 
the same way, there's no more if - else stuff.

-pu



More information about the Python-list mailing list