Replace Whole Object Through Object Method
onurb at xiludom.gro
Tue Jun 27 11:05:28 CEST 2006
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_,
It's not so clear in Python, cf my answer to Maric on this point.
> and membership tells you what a
> role _has_, and a role is something that a person has,
As a matter of fact, in Python, the class is an attribute of an object.
So it is really something that an object "have". And this relationship
is not carved in stone - it's perfectly legal to modify it at runtime.
> 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 Person(object):
> def __init__(self, name):
> self.roles = 
> self.name = name
> def add_role(self, role_class):
And here you create a circular reference between object and roles...
> def forward_to_role(self, attr):
> for role in self.roles:
> return getattr(role, attr)
> except AttributeError:
> raise AttributeError(attr)
This could as well be directly in __getattr__, and would avoid a useless
> def __getattr__(self, attr):
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