def ancestors(self, recurs=True, context=None):
<EOL>yielded = {self}<EOL>if context is None:<EOL><INDENT>context = contextmod.InferenceContext()<EOL><DEDENT>if not self.bases and self.qname() != "<STR_LIT>":<EOL><INDENT>yield builtin_lookup("<STR_LIT:object>")[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>return<EOL><DEDENT>for stmt in self.bases:<EOL><INDENT>with context.restore_path():<EOL><INDENT>try:<EOL><INDENT>for baseobj in stmt.infer(context):<EOL><INDENT>if not isinstance(baseobj, ClassDef):<EOL><INDENT>if isinstance(baseobj, bases.Instance):<EOL><INDENT>baseobj = baseobj._proxied<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if not baseobj.hide:<EOL><INDENT>if baseobj in yielded:<EOL><INDENT>continue<EOL><DEDENT>yielded.add(baseobj)<EOL>yield baseobj<EOL><DEDENT>if not recurs:<EOL><INDENT>continue<EOL><DEDENT>for grandpa in baseobj.ancestors(recurs=True, context=context):<EOL><INDENT>if grandpa is self:<EOL><INDENT>break<EOL><DEDENT>if grandpa in yielded:<EOL><INDENT>continue<EOL><DEDENT>yielded.add(grandpa)<EOL>yield grandpa<EOL><DEDENT><DEDENT><DEDENT>except exceptions.InferenceError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT>
Iterate over the base classes in prefixed depth first order.

        :param recurs: Whether to recurse or return direct ancestors only.
        :type recurs: bool

        :returns: The base classes
        :rtype: iterable(NodeNG)