How do I do this without class methods ?

Jacek Generowicz jmg at ecs.soton.ac.uk
Wed Apr 25 04:00:55 EDT 2001


I have a class hierarchy. The classes in the hierarchy differ in
(amongst other things) the value of a class variable. Furthermore, I
want to be able to select the value of said class variable from a
range of allowed values for each of the classes. I've cooked up a toy
hierarchy to illustrate the point, below.

My problem is that it seems simple enough to do with class methods,
but I don't seem to be able to find a way around their absence. What's
the pythonic way to do this ?


class woderwick:
    bwian = [ 'zero', 'one', 'two', 'many' ]
    wodger = bwian[0] # default value
    def __init__ ( self ):
        print self.wodger
    # This only affects the particular instance; no use
    def welease_bwian ( self, N ):
        self.wodger = self.bwian[N]

class rodrigo(woderwick):
    bwian = [ 'cero', 'uno', 'dos', 'demasiados' ]

class roderich(woderwick):
    bwian = [ 'gar nichts', 'eins', 'zwei', 'viele' ]  

class roderik(roderich): # :-)
    bwian = [ 'geen bal', 'een', 'twee', 'te veel' ]

class rafal(woderwick):
    bwian = [ 'figa z makiem', 'raz', 'dwa', 'kupa' ]
    
# And so on ad tedium; ie I REALLY want to write welease_bwian only
# once for the whole hierarchy. Not too much to ask, is it? as it
# performs exactly the same function in all cases.

# Now I want to be able to say
a = woderwick() # wnat zero, get zero
b = rodrigo() # want cero, get zero

# Then I want to be able to set wodger to a given element of the
# corresponding bwian, for the whole class.
        
# This only works, for the base class (no surprise)
def welease_bwian( N ):
    woderwick.wodger = woderwick.bwian[N]

welease_bwian( 1 )

a = woderwick() # Gives one, as required
b = rodrigo() # Gves one, rather than uno

# This only affects the temporary instance, hence useless
rodrigo().welease_bwian( 2 )
a = woderwick() # get one, want two
b = rodrigo()   # get one, want dos


Suggestions welcome.

Jacek



More information about the Python-list mailing list