[Tutor PyCZ] Problem s exec uvnitr funkce

Petr Prikryl PrikrylP na skil.cz
Čtvrtek Leden 5 13:51:23 CET 2006


Tomas Brabenec...
> Mam funkci s nazvem "parsetag", ktera se vola 
> rekurzivne a uvnitr te funkce mam kdesi 
> exec("_nejakyprikaz_")
> 
> Skript mi ale vyhodi nasledujici chybu:
> 
> SyntaxError: unqualified exec is not allowed 
> in function 'parsetag' it  contains
> a nested function with free variables
> 
> Nevite nekdo, co delam spatne ?

Python Reference Manual, podkapitola

4.1.1 Interaction with dynamic features 

  If exec is used in a function and the function
  contains or is a nested block with free
  variables, the compiler will raise a SyntaxError
  unless the exec explicitly specifies the local
  namespace for the exec. (In other words, "exec
  obj" would be illegal, but "exec obj in ns"
  would be legal.)
  
  [...] Names may be resolved in the local and
  global namespaces of the caller. Free variables
  are not resolved in the nearest enclosing
  namespace, but in the global namespace.
  
Zkus vše, co se v okolí exec potřebuje předávat
přes parametr té rekurzivní funkce funkce, aby to nebyly
free variables. Chtělo by to znát víc podrobností.

pepr


Další informace o konferenci Tutor