From e0b2d7ac9aea548d0bee1cdabd5d7b1254a6569d Mon Sep 17 00:00:00 2001 From: Tim Peters Date: Sat, 22 Sep 2001 06:10:55 +0000 Subject: Add a function to compute a class's method resolution order. This is easy for 2.2 new-style classes, but trickier for classic classes, and different approaches are needed "depending". The function will allow later code to treat all flavors of classes uniformly. --- Lib/inspect.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'Lib/inspect.py') diff --git a/Lib/inspect.py b/Lib/inspect.py index 1102c3b258..3febf1826d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -163,6 +163,24 @@ def getmembers(object, predicate=None): results.sort() return results +# ----------------------------------------------------------- class helpers +def _searchbases(cls, accum): + # Simulate the "classic class" search order. + if cls in accum: + return + accum.append(cls) + for base in cls.__bases__: + _searchbases(base, accum) + +def getmro(cls): + "Return tuple of base classes (including cls) in method resolution order." + if hasattr(cls, "__mro__"): + return cls.__mro__ + else: + result = [] + _searchbases(cls, result) + return tuple(result) + # -------------------------------------------------- source code extraction def indentsize(line): """Return the indent size, in spaces, at the start of a line of text.""" -- cgit v1.2.1