[Tutor] Re: conditionally defining classes

Andrei project5 at redrival.net
Thu Sep 30 19:17:52 CEST 2004


Tony Cappellini wrote on Wed, 29 Sep 2004 18:47:33 -0700 (PDT):

> I've read some posts from Guido (I think) about defining a class within a
> class. From what I remember this was frowned upon, but I would expect
> arguments on both sides.

I don't like defining things (be they functions or classes) inside other
functions/classes. It's ugly and decreases the maintainability of the code.

> 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

I agree with Jeff that you'd better define both of them and then create an
object based on one of them depending on the OS. The example above makes
particularly little sense because you a) define them in an if-statemt and
b) give them different names after all. So you'll have to implement another
if-statement to decide whether you want to create a WinXP or a Win98
object.

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.



More information about the Tutor mailing list