Replace Whole Object Through Object Method
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Mon Jun 26 19:46:48 EDT 2006
digitalorganics at gmail.com a écrit :
> Bruno Desthuilliers wrote:
(snip)
>>
>>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.
If I understand correctly, only some of the existing workers will 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.
wrt/ behaviors, it's easy as pie. Attributes (I mean instance
attributes) are another problem, but I don't have enough informations to
deal with this problem here.
> 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.
Here's a simple stupid possible solution:
class Worker(object):
def __init__(self, ...)
# init code here
# behaviours here
def becomeEmployer(self):
self.___class__ = Employer
class Employer(Worker):
# behaviours here
w = Worker(...)
w.becomeEmployer()
Note that there's no initializer in the Employer class - it wouldn't get
called anyway (not automatically at least).
More information about the Python-list
mailing list