Re: Recursive functions in py: blocks

47 views
Skip to first unread message

Pablo W

unread,
Mar 1, 2013, 5:47:51 AM3/1/13
to paste...@googlegroups.com
Le mercredi 27 février 2013 16:20:37 UTC+1, Pablo W a écrit :
Hi,

Are there some restrictions about the way python blocks are handled by tempita ?
For instance, if define a recursive function I get an error, when I try to evaluate it. Same thing if I define a function fun2 depending on a function fun1 that I have just defined.
I tried to put all the code in an exec(''' ''') block but I got the same result and I wonder why. Any idea ?

All the best,

Pablo

example:

--------- tempita doc -------------

{{py:
def fact(x):
    if x == 1:
        return x
    else:
        return x*fact(x-1)
}}


Can I do a recursive call ? {{fact(5)}}

--------------------------------

The error message is

--------------------------------

Traceback (most recent call last):
  File "run_template.py", line 7, in <module>
    code = template.substitute()
  File "/home/pablo/Programmation/python/science/local/lib/python2.7/site-packages/tempita/__init__.py", line 173, in substitute
    result, defs, inherit = self._interpret(ns)
  File "/home/pablo/Programmation/python/science/local/lib/python2.7/site-packages/tempita/__init__.py", line 184, in _interpret
    self._interpret_codes(self._parsed, ns, out=parts, defs=defs)
  File "/home/pablo/Programmation/python/science/local/lib/python2.7/site-packages/tempita/__init__.py", line 212, in _interpret_codes
    self._interpret_code(item, ns, out, defs)
  File "/home/pablo/Programmation/python/science/local/lib/python2.7/site-packages/tempita/__init__.py", line 232, in _interpret_code
    base = self._eval(parts[0], ns, pos)
  File "/home/pablo/Programmation/python/science/local/lib/python2.7/site-packages/tempita/__init__.py", line 294, in _eval
    value = eval(code, self.default_namespace, ns)
  File "<string>", line 1, in <module>
  File "<string>", line 5, in fact
NameError: global name 'fact' is not defined at line 10 column 31 in file tempita_output




Reply all
Reply to author
Forward
0 new messages