How do I do this without class methods ?

Jacek Generowicz jmg at ecs.soton.ac.uk
Wed Apr 25 08:03:32 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> writes:

> If attributes with the same name are set on both
> instance and class, and you access that name through
> an instance, the class attribute will not be fetched
> (unless you explicitly ask for it, self.__class__.wodger).

The last line is intersting new information, thanks.

> If as it seems you're always accessing attribute .wodger from
> instances, never classes, and only ever wants to set it on a
> per-class basis instead (so all the instances extant get affected)

This is almost exactly what I want (apart from an edifying
discussion around the subject - for your contribution to which, I
thank you)

Specifically, what I want to be able to do (in context of the toy
model) is to say `Look, guys, today's number is N', before any
instances of the woderwicks and their relatives are created, and then
have everyone (those already extant, and those yet to be born)
pronounce that number in his own fashion whenever he is called upon to
do so:

w = woderwick()    -> zero
r = rodrigo()      -> cero

and_the_Lord_said_let_the_number_be( 2 )

woderwick()        -> two
rodrigo()          -> dos
print w.wodger     -> two
print r.wodger     -> dos

> maybe what you need is a different approach:
>
> class woderwick:
>     bwian = [ 'zero', 'one', 'two', 'many' ]
>     _wodger_index = 0 # default index
>     def __getattr__(self, name):
>         if name=='wodger':
>             return self.bwian[self._wodger_index]
>         raise AttributeError, name
>     def __init__ ( self ):
>         print self.wodger

So far, great . . .

>     # Affect class through instance...
>     def welease_bwian ( self, N ):
>         self.__class__._wodger_index = N

. . . but this is not quite what I want; instead I would like to be
able to change the index for ALL classes in the hierarchy in one go.

This should do the trick:

def welease_bwian( index ):
    woderwick._wodger_index = index

Hmm . . . this whole example is making me misspell return as weturn.

Thanks,

Jacek

(Full version performing to spec. below . . .)

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

class woderwick:
    bwian = [ 'zero', 'one', 'two', 'many' ]
    _wodger_index = 0 # default
    def __getattr__ ( self, name ):
        if name == 'wodger':
            return self.bwian[self._wodger_index]
        raise AttributeError, name
    def __init__ ( self ):
        print self.wodger

def welease_bwian( index ):
    woderwick._wodger_index = index

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 work
w = woderwick() # zero
r = rodrigo() # cero

# Change number for all present and future instances
welease_bwian( 1 )

print w.wodger # one
woderwick() # one

print r.wodger # uno
rodrigo() # uno 



More information about the Python-list mailing list