exec command does not work in python

Asked

Viewed 492 times

1

Well, I’m not able to perform external functions (with exec) that will change global lists to be used in my main function, follows code:

m = []
n = []

def mp((a,b),p):
   c = [n[0] for n in p]
   d = [z.index for z in c if (a == z)]

   if len(d) == 1 and ((((sum(p) - p[d[0]]) + b) % 5) == 0 ):
           return m.append((((sum(p) - p[d[0]]) + b),d[0]))
   elif len(d) > 1 :
       return ( m.append((((sum(p) - p[d[i]]) + b),d[i])) for i in d if ((sum(p) - p[d[i]]) + b) % 5 == 0 ) 
   elif ( len(d) == 0 ):
       return m.append(None)

def mp2((a,b),p):
   c = [n[0] for n in p]
   e = [k.index for k in c if (b == z)]

   if len(e) == 1 and ((((sum(p) - p[e[0]]) + a) % 5) == 0 ):
       return  n.append((((sum(p) - p[e[0]]) + a),e[0]))
   elif len(e) > 1 :
       return ( n.append((((sum(p) - p[e[i]]) + a),e[i])) for i in e if ((sum(p) - p[e[i]]) + a) % 5 == 0 ) 
   elif ( len(e) == 0 ):
       return n.append(None)

def maior_ponto((a,b),p):
   g = mp((a,b),p)
   h = mp2((a,b),p)
   exec(g)
   exec(h)
   u = (m+n)
   return(r[1] for r in u if max(r[0]))

The error returned in the terminal is:

python bloco313.py
  File "bloco313.py", line 71
    exec(g)
SyntaxError: unqualified exec is not allowed in function 'maior_ponto' it contains a nested function with free variables

What’s the problem in the function? I didn’t identify variable problem. I’m Using python 2.7.5 on the Ubuntu terminal

  • What you call the function maior_ponto? By the way, try to name your variables better and functions with self-explanatory names.

1 answer

1

In Python 2.x, the exec function cannot appear in functions that have "functions" with free variables. A generating expression implicitly defines an object code for the code that must be executed in each iteration of that function. In function maior_ponto you call the "free variable" max() within the generating expression (max within the return, in this case), which prevents this function from using exec.

The following code (without max), for example, it works:

def maior_ponto((a,b),p):
   g = mp((a,b),p)
   h = mp2((a,b),p)
   exec(g)
   exec(h)
   u = (m+n)
   return(r[1] for r in u if r[0])

In Python 3.x the function exec has been amended and should no longer present this type of error.

Response taken from Stackoverflow in English: Explanation of exec behavior

  • oww, thanks for the help, really the mistake was that... actually, on the issue of job appointment, it’s an academic work I’m doing, with predefined function names. Since I need to run mp and mp2 before Return, what should I do? mp and mp2 change global variables that I will use in the return, where I need to return in the generation of d-uplas, the r[0] element of the r[1 maximum].

  • Can’t you return the whole element u and check the r[1] out of action? It’s not the ideal solution, but it should work. Ps: if the answer is right please accept!

  • 1

    @N4dr would be nice if you dial and accept that answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.