J
Jeffrey Barish
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.
Most of my program lives in a class. My plan is to have a superclass
that performs the generic functions and subclasses to define methods
specific to each platform. I envision something like this:
class super:
'''All the generic stuff goes here'''
class linux_subclass(super):
def func(self):
'''linux-specific function defined here'''
class windows_subclass(super):
def func(self):
'''windows-specific function defined here'''
And then in main I have:
inst = eval('%s_subclass' % sys.platform)(args)
to create an instance of the appropriate subclass.
I realize that I will have to name the subclasses appropriately
depending on what values get assigned to sys.platform. (On my platform,
sys.platform is 'linux2', so I would actually need class
linux2_subclass(super). I don't know what value gets assigned on a
windows platform.)
The other way I thought of -- surrounding all the platform-specific code
with if statements to test sys.platform -- seems clearly worse. I'm
just getting up to speed on Python and OOP, so I'm wondering whether I
have missed something obvious. I'm hoping that the strongest rebuke
would be that I found something obvious.
(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.
Most of my program lives in a class. My plan is to have a superclass
that performs the generic functions and subclasses to define methods
specific to each platform. I envision something like this:
class super:
'''All the generic stuff goes here'''
class linux_subclass(super):
def func(self):
'''linux-specific function defined here'''
class windows_subclass(super):
def func(self):
'''windows-specific function defined here'''
And then in main I have:
inst = eval('%s_subclass' % sys.platform)(args)
to create an instance of the appropriate subclass.
I realize that I will have to name the subclasses appropriately
depending on what values get assigned to sys.platform. (On my platform,
sys.platform is 'linux2', so I would actually need class
linux2_subclass(super). I don't know what value gets assigned on a
windows platform.)
The other way I thought of -- surrounding all the platform-specific code
with if statements to test sys.platform -- seems clearly worse. I'm
just getting up to speed on Python and OOP, so I'm wondering whether I
have missed something obvious. I'm hoping that the strongest rebuke
would be that I found something obvious.