[Python-ideas] inspect.getclassdistance

Steven D'Aprano steve at pearwood.info
Mon Jan 5 12:43:15 CET 2015


On Mon, Jan 05, 2015 at 11:20:11AM +0000, Alexis Lee wrote:
> Hopefully sufficiently documented to stand without introduction:

Er, not really. What does distance mean in the context of classes? When 
would you use this? Can you give some examples, e.g. what's the distance 
between int and str?

>     def getclassdistance(srcs, dst):
>         """srcs may be either a single class or a list of (class, distance) pairs.
>         dst is the superclass to find.
>         Performs a breadth-first search for dst, returning the shortest distance.
>         """
>         if type(srcs) != list:
>             srcs = [(srcs, 0)]
>         if len(srcs) == 0:
>             return None

Shouldn't that be an exception?


>         here, distance = srcs[0]
>         if here == dst:
>             return distance
>         newSrc = srcs[1:] + [(c, distance + 1) for c in list(here.__bases__)]
>         return classDistance(newSrc, dst)

What's classDistance?


> If this is already implemented and I just couldn't find it, I'm more
> than happy to withdraw the idea.

Explain it first :-)


-- 
Steven


More information about the Python-ideas mailing list