Movatterモバイル変換


[0]ホーム

URL:


How do I do this without class methods ?

Jacek Generowiczjmg 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, Iwant to be able to select the value of said class variable from arange of allowed values for each of the classes. I've cooked up a toyhierarchy 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'sthe 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 saya = woderwick() # wnat zero, get zerob = 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 requiredb = rodrigo() # Gves one, rather than uno# This only affects the temporary instance, hence uselessrodrigo().welease_bwian( 2 )a = woderwick() # get one, want twob = rodrigo()   # get one, want dosSuggestions welcome.Jacek


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp