How do I do this without class methods ?

Jacek Generowicz jmg at ecs.soton.ac.uk
Wed Apr 25 05:38:23 EDT 2001


Martin von Loewis <loewis at informatik.hu-berlin.de> writes:

> def welease_bwian(klasse, N):
>   klasse.wodger = klasse.bwian[N]

Cool, I like this. I guessed I should be able to do something along these
lines . . . but it still leaves me with some problems.

A call to welease_bwian( woderwick, ... ) incorrectly sets wodger for
all the subclasses.

-----------------------------------------------------------

def welease_bwian( the_class, N ):
    the_class.wodger = the_class.bwian[N]

class woderwick:
    bwian = [ 'zero', 'one', 'two', 'many' ]
    wodger = bwian[0] # default value
    def __init__ ( self ):
        print self.wodger

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' ]
    
# Defaults still don't work
a = woderwick() # wnat zero, get zero
b = rodrigo() # want cero, get zero

# Base class call incorrectly sets subclass wodger
welease_bwian( woderwick, 1 )
a = woderwick() # Gives one, as required
b = rodrigo() # Gves one, rather than uno

welease_bwian( rodrigo, 1 )
c = rodrigo() # Now correctly gives uno, but tedious to do for all subclasses.
              # Alternatively, requres knowledge of which particular class
              # I am using (which I don't know how to get).





More information about the Python-list mailing list