[FRPythoneers] instance question

Mike Olson Mike.Olson at fourthought.com
Sun Mar 17 11:47:53 MST 2002


On Sun, 2002-03-17 at 11:31, Karl Putland wrote:
> 
> It would be ideal if this function would return the Super classes
> in the correct class search order.

It does.  The only thing you would need to change is

instances = [klass]

and remove the appedd from in the loop to get the entire search path. 
The difference being that the initial class was not in the search list. 
Python does left-to-right depth first.  It will search all of the way
down the left most branch first, then start working its way right from
the bottom of the tree.

>>> class A:
...   x=1
...
>>> class B:
...   x=2
...
>>> class C(A,B):
...   pass
...
>>> class D:
...   x = 3
...
>>>
>>> class E(C,D):
...   pass
...
>>>
>>> inst = E()
>>> print inst.x
1

The search order in the above is:
E, C, A, B, D

>>> def Flatten(klass):
...   instance = [klass]
...   for sub in klass.__bases__:
...     instance.extend(Flatten(sub))
...   return instance
...
>>> print Flatten(inst.__class__)
[<class __main__.E at 0x816da44>, <class __main__.C at 0x816df4c>,
<class __main__.A at 0x816d17c>, <class __main__.B at 0x816d344>, <class
__main__.D at 0x816e1ac>]


Mike

> 
> --Karl
> 
> --- Mike Olson <Mike.Olson at fourthought.com> wrote:
> > On Sun, 2002-03-17 at 10:39, Evelyn Mitchell wrote:
> > > I know you can use isinstance(object, class) to find out if
> > object is an
> > > instance of class (or of a subclass of class).
> > > 
> > > Is there any way to find out all the classes that object may be
> > an instance
> > > of?
> > > 
> > > Bicycle Repair is streching me again.
> > 
> > You would need to walk the tree by hand, or write a quick routine
> > to
> > flatten it.  I don't know of anything builtin
> > 
> > def Flatten(klass):
> >     instances = []
> >     for sub in klass.__bases__:
> >         instances.append(sub)
> >         instances.extend(Flatten(sub))
> >     return instances
> > 
> > print Flatten(inst)
> > 
> > Mike
> > 
> > > 
> > > -- 
> > > Regards,                    tummy.com, ltd 
> > > Evelyn Mitchell             Linux Consulting since 1995
> > > efm at tummy.com               Senior System and Network
> > Administrators
> > >                             http://www.tummy.com/
> > > _______________________________________________
> > > This message sent by the FRPythoneers mailing list.
> > > Unsubscribe: echo unsubscribe |
> > FRPythoneers-request at lists.community.tummy.com
> > > URL:
> > http://lists.community.tummy.com/mailman/listinfo/frpythoneers
> > -- 
> > Mike Olson                                Principal Consultant
> > mike.olson at fourthought.com                +1 303 583 9900 x 102
> > Fourthought, Inc.                         http://Fourthought.com 
> > 4735 East Walnut St,                      http://4Suite.org
> > Boulder, CO 80301-2537, USA
> > XML strategy, XML tools, knowledge management
> > 
> > _______________________________________________
> > This message sent by the FRPythoneers mailing list.
> > Unsubscribe: echo unsubscribe |
> > FRPythoneers-request at lists.community.tummy.com
> > URL:
> http://lists.community.tummy.com/mailman/listinfo/frpythoneers
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Sports - live college hoops coverage
> http://sports.yahoo.com/
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
-- 
Mike Olson                                Principal Consultant
mike.olson at fourthought.com                +1 303 583 9900 x 102
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St,                      http://4Suite.org
Boulder, CO 80301-2537, USA
XML strategy, XML tools, knowledge management




More information about the FRPythoneers mailing list