Replace Whole Object Through Object Method

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 11:57:33 EDT 2006


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

def isClass(object):
    if 'classobj' in str(type(object)):
        return 1
    elif "'type'" in str(type(object)):
        return 1
    else:
        return 0
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,)
------------------------------------------------------------------------

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...




More information about the Python-list mailing list