On Dec 4, 11:16 am, "Zac Burns" <[EMAIL PROTECTED]> wrote: > The class method seems to be the most promising, however I have more > 'state' methods to worry about so I might end up building new classes > on the fly rather than have a class per permutation of states! Now the > code isn't quite as clear as I thought it was going to be. > > It seems unfortunate to me that methods are always looked up on the > class for new style objects. Was this done for speed reasons?
I thought of two more solutions. One, derive a new class for each instance as you create it, and assign methods to that, that can be shared. Even more memory consumption though. >>> class A: ... def methA( self ): print 'methA' ... def methB( self ): print 'methB' ... >>> class A1(A): meth= A.methA ... >>> a1=A1() >>> a1.meth() methA >>> A1.meth=A.methB >>> a1.meth() methB Two, use getter properties to return the right function, based on index and array. >>> class A( object ): ... @property ... def meth( self ): ... return self.meths[ self.methA_i ].__get__( self, A ) ... def methA( self ): ... print 'methA' ... self.methA_i+= 1 ... def methB( self ): ... print 'methB' ... self.methA_i-= 1 ... meths= [ methA, methB ] ... def __init__( self ): ... self.methA_i= 0 ... >>> a= A() >>> a.meth() methA >>> a.meth() methB >>> a.meth() methA Or (two B), look up the method by name on name-index pair. >>> class A( object ): ... @property ... def meth( self ): ... return getattr( self, self.meths[ self.methA_i ] ) ... def methA( self ): ... print 'methA' ... self.methA_i+= 1 ... def methB( self ): ... print 'methB' ... self.methA_i-= 1 ... meths= [ 'methA', 'methB' ] ... def __init__( self ): ... self.methA_i= 0 ... >>> a= A() >>> a.meth() methA >>> a.meth() methB >>> a.meth() methA The 'meths' list will need separate lists for each 'group' of state- dependent functions you wish to use. -- http://mail.python.org/mailman/listinfo/python-list