Replace Whole Object Through Object Method

Bruno Desthuilliers onurb at xiludom.gro
Mon Jun 26 19:02:37 CEST 2006


digitalorganics at gmail.com wrote:
> How can an object replace itself using its own method? 

AFAIK, It can't (but I can be wrong - some guru around ?).

> 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

FWIW:
Python 2.4.3 (#1, Jun  3 2006, 17:26:11)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def dumbfactory():
...     class Dumb(object): pass
...     class Dummy: pass
...     return Dumb, Dummy
...
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__doc__': None, 'dumbfactory': <function dumbfactory at
0x2aaaaab66e60>}
>>> def fun():
...     dumb, dummy = dumbfactory()
...     return
...
>>> fun()
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'fun': <function fun at 0x2aaaaab66ed8>, '__doc__': None,
'dumbfactory': <function dumbfactory at 0x2aaaaab66e60>}
>>>

Looks like dumb and dummy won't get listed... And also:

>>> class Mymeta(type):
...     pass
...
>>> class Foo(object):
...     __metaclass__ = Mymeta
...
>>> "'type'" in str(type(globals()['Mymeta']))
True

Looks like this will list metaclasses too... May or may not be a problem...

> def MixInto(Class, Mixin):

You're aware that in this function's scope, the 'Mixin' arg name will
shadow the Mixin class name ? (sorry for asking dumb question).

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

Instead of exposing problems with your solution, you may want to expose
the real use case ?


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list