title: Python: replace function by its expression, in mathematical expression as string, with Sympy?
body: <p>I have a function definition, as a string, say <code>func_def = "f(x, y) = x + 2*y + 3"</code>.<br>
  I have a mathematical expression, as a string, including that function, say<br>
  <code>expr = '2*a*b + f(b+a, 2*b)'</code>.   </p>
  
  <p>How can I replace, in my expression, the function by its value?<br>
  In other words, how do I get a string <code>expr_eval = "2*a*b + ( (b+a) + 2*(2*b) + 3 )"</code>?</p>
  
  <p>Is there an easy solution for this, for example with Sympy using a combination of <code>sympify</code> and <code>subs</code> and some other function? I feel there should be one, but can't find it. I have to do this for quite a few equations containing many symbols, so that creating Sympy symbols for each of them separately does not seem like a great option.</p>
  
  <p>For now, I use regex, but I find this solution complicated to come up with, to generalise (other functions with other numbers of variables), to scale, and to read. It goes like this:   </p>
  
  <pre class="lang-py prettyprint-override"><code>import re
  
  func_def = "f(x, y) = x + 2*y + 3"
  expr = '2*a*b + f(b+a, 2*b)'
  
  # extract substring with function
  str_to_replace = re.findall(r'f\([a-zA-z0-9\*\+\"\']*, [a-zA-z0-9\*\+\"\'/]*\)', expr)
  str_to_replace = str_to_replace[0]
  
  # extract function name and arguments in substring
  func_name, args, _ = re.split('\(|\)', str_to_replace)
  args = args.split(',')
  args = [i.strip() for i in args]
  
  # parse the function definition
  func_def_lhs, func_def_rhs = func_def.split('=')
  func_def_name, args_def, _ = re.split('\(|\)', func_def_lhs)
  args_def = args_def.split(',')
  args_def = [i.strip() for i in args_def]
  
  # replace each argument in the definition by its expression
  for i, arg_def in enumerate(args_def) : 
  
      func_def_rhs = func_def_rhs.replace(arg_def, '({})' .format(args[i]))
  
  expr_eval = expr.replace(str_to_replace, '({})' .format(func_def_rhs))
  
  </code></pre>