Instead of bases and the while loop I suggest you just iterate over f.__class__.mro()