Replace Whole Object Through Object Method

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 13:19:00 EDT 2006


Maric Michaud wrote:
> Le lundi 26 juin 2006 17:57, digitalorganics at gmail.com 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.

You mean "object"? The shadow/mask only exists within the scope of the
function. But anyhow, point well taken.

> >     if 'classobj' in str(type(object)):
> Why don't you test the type directly ?

Thanks.

> >         return 1
> Python has boolean for clarity.

Thanks.

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

Very clean! Thank you.

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

What doesn't work exactly? The whole purpose of the mixin is to add
functionality to the class and hence to all its instances on the fly.
Creating a new type would not achieve this, unless there's something
I'm missing (a very real possibility!). And what do you mean doesn't
work in most newstyleclass cases? Seems to be working just fine...

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

Any answers my primary question though?

> >
> > Advance thanks to all who can help...
>
> --
> _____________
>
> Maric Michaud
> _____________
>
> Aristote - www.aristote.info
> 3 place des tapis
> 69004 Lyon
> Tel: +33 426 880 097




More information about the Python-list mailing list