Le lundi 26 juin 2006 17:57, [EMAIL PROTECTED] a écrit : > How can an object replace itself using its own method? See the > following code: > > class Mixin: > def mixin(object, *classes): > NewClass = type('Mixin', (object.__class__,) + classes, {}) > newobj = NewClass() > newobj.__dict__.update(object.__dict__) > return newobj >
Variables in python are names, not the objects, and instances shouldn't know nothing about how they are referenced. I guess what you want to do is, in fact, very simple somethig like : a = SomeClass() a = a.some_method_wich_return_a_new_object() or : for k, v in globals().iteritems() : if isinstance(v, SomeClass) : globlals()[k] = v.some_method_wich_return_a_new_object() > def isClass(object): Don't mask builtin names. > if 'classobj' in str(type(object)): Why don't you test the type directly ? > return 1 Python has boolean for clarity. > elif "'type'" in str(type(object)): > return 1 > else: > return 0 should be : import types def isClass(object_) : if isinstance(object_, type) : return True # new style class elif isinstance(object_, types.ClassType) : return True # old-style class else : return False or if you don't need to diferentiate the cases : def isClass(object_) : return isinstance(object_, type) or \ isinstance(object_, types.ClassType) > def listClasses(): > classes = [] > for eachobj in globals().keys(): > if isClass(globals()[eachobj]): > classes.append(globals()[eachobj]) > print eachobj > return classes > > def MixInto(Class, Mixin): > if Mixin not in Class.__bases__: > Class.__bases__ += (Mixin,) This doesn't work in most cases (with new style classes), better recreat a type which inherit from Class and Mixin, or Class.__dict__ with Mixin.__dict__. > ------------------------------------------------------------------------ > > Okay, so the mixin function becomes part of whatever class I choose and > hence its instances, but the problem is that the way I currently have > it setup mixin() returns a new object, instead of replacing whatever > class instance that calls it into that new object. I hope I'm making > sense here. > > Basically what I need is for the method to be able to find out the name > of the instance, then I can just go to the globals dictionary to do the > replacement. > > Advance thanks to all who can help... -- _____________ Maric Michaud _____________ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list