title: Calling a Python class method from C++, if given an initialised class as PyObject
body: <p>I have a function in c++ that receives a initialised class as a PyObject.
  The python class is:</p>
  
  <pre class="lang-py prettyprint-override"><code>class Expression:
      def __init__(self, obj):
          self.obj = obj
  
      def get_source(self):
          #Check if the object whose source is being obtained is a function.
          if inspect.isfunction(self.obj):
              source = inspect.getsourcelines(self.obj)[0][1:]
              ls = len(source[0]) - len(source[0].lstrip())
              source = [line[ls:] for line in source]
              #get rid of comments from the source
              source = [item for item in source if item.lstrip()[0] != '#']
              source = ''.join(source)
              return source
          else:
              raise Exception("Expression object is not a function.")
  </code></pre>
  
  <p>The c++ receives this:</p>
  
  <pre class="lang-py prettyprint-override"><code>Expression(somefunctogetsource)
  </code></pre>
  
  <p>From c++ how do I call the get_source method of the expression object?
  So far I've read the python c-api docs and tried things like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>PyObject* baseClass = (PyObject*)expression-&gt;ob_type;
  PyObject* func = PyObject_GetAttrString(baseClass, "get_source");
  PyObject* result = PyObject_CallFunctionObjArgs(func, expression, NULL);
  </code></pre>
  
  <p>And convert the result to a string, but this doesn't work.</p>