[Python-ideas] inspect.getclassdistance

Andrew Barnert abarnert at yahoo.com
Mon Jan 5 13:09:37 CET 2015

On Jan 5, 2015, at 12:20, Alexis Lee <alexisl at hp.com> wrote:

> Hopefully sufficiently documented to stand without introduction:

It doesn't actually run as written (maybe you just used the wrong name in what was supposed to be a recursive call?), and I'm not sure you're doing the BFS right, but I think I can guess what you're after:

You're trying to find out if dst is an ancestor of src (the option of taking a list of pairs, despite being documented, seems to be there only for internal use by recursive calls) and, if so, how long the path is between them. And:

 * Only actual inheritance counts; abc.register or other subclass hooks are ignored.
 * In case of diamond inheritance where the sides are of unequal length, the shorter one counts (even if that shorter path would never be followed by the c3 rule and therefore isn't even relevant at runtime).
 * More generally, you don't care about the c3 MRO path, or the actual path taken for any particular method's lookup, just the inheritance path itself.
 * The actual classes on the path are irrelevant to you, you just want its length.

Also, it seems like it would be simpler to just create a general inheritance-BFS-walker function that yields each base as it goes, which would allow you to write this function as a one-liner.

I also don't get why you're using recursion rather than a loop here, given that there's nothing interesting on the stack frames. I suspect you wrote this in Common Lisp or OCaml first and then ported it to Python? That might also explain why it's such a strange fit for the way inheritance works in Python.

But, most importantly, if I'm right about what this does, I can't think of anywhere I'd want to use it. I can imagine cases (although not very Pythonic ones) where I'd want to know, say, the MRO path (as in dst.__mro__[:dst.__mro__.find(src)][::-1]), but this... When would it ever be interesting?

>    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
>        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)
> If this is already implemented and I just couldn't find it, I'm more
> than happy to withdraw the idea.
> Alexis
> -- 
> Nova Engineer, HP Cloud.  AKA lealexis, lxsli.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list