On Jan 5, 2015, at 12:20, Alexis Lee firstname.lastname@example.org 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:
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 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.
Nova Engineer, HP Cloud. AKA lealexis, lxsli.
Python-ideas mailing list Pythonemail@example.com https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/