title: Python ast: decide whether a FunctionDef is inside a ClassDef or not
body: <p>I'd like to build an ast from a Python source code, then get specific information from the ast. I faced the following problem: while walking through a ClassDef's body is viable, how I decide if a method is inside the class or not.</p>
  <p>The code I build ast from:</p>
  <pre class="lang-py prettyprint-override"><code>class A:
      def foo(self):
          pass
  
  
  def foo(self):
      pass
  
  </code></pre>
  <p>In this example I will hit all the <code>foo</code>s but I will not able to tell if it is from the class or not (as they have the same set of parameters, named badly, but the code can be interpreted).</p>
  <pre class="lang-py prettyprint-override"><code>    def build_ast(self):
          with open(self.path, 'r', encoding='utf-8') as fp:
              tree = ast.parse(fp.read())
              for node in ast.walk(tree):
                  if isinstance(node, ast.FunctionDef):
                      print(ast.dump(node))
                      # access the parent if it has
  </code></pre>