Replace Whole Object Through Object Method

digitalorganics at gmail.com digitalorganics at gmail.com
Mon Jun 26 13:29:01 EDT 2006


Bruno Desthuilliers wrote:
> 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 ?).
>
> > ...
>
> 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).

No sorry necessary, but yes, I am aware of it. Poor programming
practice I'm sure...

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

***
I'm working with a team that's doing social modeling, and for example,
I need to model workers that at some point in the program may or may
not also become employers. Now, I want the workers to take on all
behaviors and attributes of an employer in addition to their
pre-existing "worker" behaviors and attributes. Also, as I'm sure you
guessed, the workers' attributes need to retain their values at that
point in the program, so a brand new worker-employer object wouldn't in
itself do the trick.
***

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