Point well taken, and indeed a brilliant solution. Thank you I V for demonstrating so clearly.
I V wrote: > On Mon, 26 Jun 2006 19:40:52 -0700, digitalorganics wrote: > > A misuse of inheritance eh? Inheritance, like other language features, > > is merely a tool. I happen to be using this tool to have my virtual > > persons change roles at different points in their lifetime, as many > > real people tend to do. Thus, at these points, B is indeed an A. What a > > person is, whether in real life or in my program, is not static and > > comes into definition uniquely for each moment (micro-moment, etc.) of > > existence. Now, please, I have no intention of carrying the > > conversation in such a silly direction, I wasn't inviting a discussion > > on philosophy or some such. I seek to work the tools to my needs, not > > the other way around. > > But thinking about the problem in the vocabulary provided by the > programming language can be helpful in coming up with a solution. If > inheritance tells you what an object _is_, and membership tells you what a > role _has_, and a role is something that a person has, that suggests > that an implementation where roles are members of a person might be > simpler than trying to use inheritance. Like, for instance: > > class Role(object): > def __init__(self, person): > self.person = person > > > class Worker(Role): > def do_work(self): > print self.name, "is working" > > > class Employer(Role): > > def __init__(self, person): > Role.__init__(self, person) > self.employees = [] > > > def add_employee(self, worker): > self.employees.append(worker) > > > def boss_people_around(self): > for employee in employees: > print self.name, "is telling", employee.name, "what to > do" > > > class Person(object): > > def __init__(self, name): > self.roles = [] > self.name = name > > > def add_role(self, role_class): > self.roles.append(role_class(self)) > > > def forward_to_role(self, attr): > for role in self.roles: > try: > return getattr(role, attr) > except AttributeError: > pass > raise AttributeError(attr) > > > def __getattr__(self, attr): > self.forward_to_role(attr) > > > bill = Person('Bill') > bill.add_role(Worker) > > bob = Person('Bob') > bob.add_role(Employer) > > bill.work() > bob.boss_people_around() -- http://mail.python.org/mailman/listinfo/python-list