[Tutor] conditionally defining classes
Jeff Shannon
jeff at ccvcorp.com
Thu Sep 30 04:07:29 CEST 2004
Tony Cappellini wrote:
>Going off in another direction- is it Pythonic to conditionally define a
>class as in ..
>
>import sys
>OSVer= sys.getwindowsversion()[0]
>
>if OSVer == 5:
> class OSWinXp(object):
> def __init__(self):
> # XP specific code goes here
>elif OSVer == 4:
> class OsWin98(object):
> def __init__(self):
> # W98 specifc code goes here
>
>
That seems acceptable to me, but I'd probably define both classes and
then use only the appropriate one --
class OSWinXp(object):
# stuff
class OSWin98(object):
# stuff
if OSVer == 5:
OSWin = OSWinXp
else:
OSWin = OSWin98
my_os = OSWin()
If you stuff all of the conditional stuff inside of a module, then you
get a nice clean interface, with the client code never needing to
know/care about the details.
import OSWin
my_os = OSWin.OSWin()
This is roughly how the built-in os module works...
Jeff Shannon
Technician/Programmer
Credit International
More information about the Tutor
mailing list