Find the closest relative
7stud
bbxx789_05ss at yahoo.com
Fri May 25 03:40:50 EDT 2007
On May 25, 12:31 am, "jm.sur... at no.spam.gmail.com"
<jm.sur... at gmail.com> wrote:
> This is how I implemented; I guess there must be elegant way to do
> this...
>
> def find_closest_relative(a,b,c):
> c1 = b.__class__
> c2 = b.__class__
>
> while True:
> if isinstance(a, c1):
> return b
> if isinstance(a, c2):
> return c
> c1 = c1.__base__
> c2 = c1.__base__
>
> -
> Suresh
I can't see how your code does what you describe.
> Now, given one of the instance, I want to find the
> closest relative of the other two.
What influence would an object have over the closest relative of two
other objects? The closet relative of two other objects is
independent of any third object. Do you want to find the closest
relative of 3 objects? If so, this might work:
import inspect
class A(object): pass
class X(object): pass
class B(A, X): pass #an object of this class has A as a base class
class C(A, X): pass
class D(A, X): pass
class E(C): pass #an object of this class has A as a base class
class F(D): pass #an object of this class has A as a base class
def closestRelative(x, y, z):
b1 = inspect.getmro(x.__class__)
b2 = inspect.getmro(y.__class__)
b3 = inspect.getmro(z.__class__)
for elmt in b1:
if elmt in b2 and elmt in b3:
return elmt
return None
b = B()
e = E()
f = F()
print closestRelative(b, e, f)
However, you should probably post an example of a class structure and
describe what you want to happen when you have three instance of the
various classes.
More information about the Python-list
mailing list