# Calling Python function from C++

**URL:** <https://root-forum.cern.ch/t/calling-python-function-from-c/37656>\
**Category:** Newbie\
**Created:** [January 21, 2020, 3:30pm UTC](https://root-forum.cern.ch/t/calling-python-function-from-c/37656 "2020-01-21T15:30:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [January 21, 2020, 3:30pm UTC](https://root-forum.cern.ch/t/calling-python-function-from-c/37656/1 "2020-01-21T15:30:38Z")

</div>

I recall somewhere saying that ROOT supports calling Python functions from C++ by converting functions into std::function. Is this information correct? If it is, how can I do so? My experiments kept failing.

I have tried passing a lambda as a std::function. It doesn’t work

```auto
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("int foo(function<int(int,int)> f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int ::foo(function<int(int,int)> f) =>
    could not convert argument 1

```

Template also doesn’t work

```auto
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("template <typename T>int foo(const T& f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can not resolve method template call for 'foo'

```

Nor function pointers work

```auto
>>> import ROOT
>>> gROOT = ROOT.gROOT
>>> gROOT.ProcessLine("using func_t = int(int,int);")
0
>>> gROOT.ProcessLine("int foo(func_t f) { return f(1, 1); }")
0
>>> ROOT.foo(lambda a, b: a+b)
<stdin>:1: RuntimeWarning: creating converter for unknown type "int(int,int)"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int ::foo(int(int,int) f) =>
    could not convert argument 1

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [January 21, 2020, 5:18pm UTC](https://root-forum.cern.ch/t/calling-python-function-from-c/37656/2 "2020-01-21T17:18:12Z")

</div>

> [@marty1885](#):
>
> I recall somewhere saying that ROOT supports calling Python functions from C++ by converting functions into std::function. Is this information correct?

Hi Martin,  
I don’t think that’s correct. The other way around (calling C++ functions from python) works transparently for most simple cases. To invoke python functions you need all of python’s context (and you need to do it through a python interpreter, because in general there is no binary that C++ can hook into). That’s what `TPython::Eval` and `TPython::Exec` help with (docs [here](https://root.cern.ch/doc/master/classTPython.html)), and for simple return types `Eval` actually does what you need.

Hope this helps,  
Enrico

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [January 22, 2020, 1:02am UTC](https://root-forum.cern.ch/t/calling-python-function-from-c/37656/3 "2020-01-22T01:02:49Z")

</div>

A priori, you’re going to need experimental\_pyroot, but although it supports conversion to std::function and to C-style function pointer, it does not do so for templates (i.e. python callable -\> std::function is not considered when matching the template). Is a good idea, though.

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [January 22, 2020, 1:19am UTC](https://root-forum.cern.ch/t/calling-python-function-from-c/37656/4 "2020-01-22T01:19:19Z")

</div>

Thank you all. Seems I got my information from a slide on the internet not realizing it is in the experimental branch.
