1

I try create a function with a loop for inside. The script work without function but declare the python function don't work. (The original script is is more longer but with this part i think the is enough)

import numpy as np
import math as mt
from sympy import*
import fractions
init_printing(use_latex='mathjax')
PHnumbers=4
PHnumbers2=2
statetype= 1
for d in range(1,9):                                                                                                     
    modes=d+1
    if statetype==1:                                                                                                 
       comb=modes 
    elif statetype==2:
        comb=int((modes*(modes+1))/2)
    exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
    for i in range(modes):
        exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')                                                          
        exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])') 
        exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')                                           
        exec(f'v{i+1}=[[0]]*modes')                                                                                        
        exec(f'Phasesv{d}[0]*=0')
    for i in range(modes):
        exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')                     
    conteo = d
    for j in range(modes):
        for i in range(modes):
            if j<i:
                conteo = conteo + 1
                exec(f'v{conteo+1}=[[0]]*modes')
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')  
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')  
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
        exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')

for i in range(len(Vec4)): print(Vec4[i]) The previous script work, no problems up to here. Now I declare python function:

def metro(PHnumbers):                                                                                                           
    statetype=1
    PHnumbers2=2
    for d in range(1,9):                                                                                                     
        modes=d+1
    if statetype==1:                                                                                                 
        comb=modes 
    elif statetype==2:
        comb=int((modes*(modes+1))/2)
    exec(f'Phases{d}=[]'), exec(f'Phasesv{d}=[]')
    for i in range(modes):
        exec(f'theta_{i+1}= symbols(\'theta_{i+1}\', real=True)')                                                          
        exec(f'Phases{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])') 
        exec(f'Phasesv{d}.append(globals()[\'\'.join([\'theta_\',str(i+1)])])')                                           
        exec(f'v{i+1}=[[0]]*modes')                                                                                        
        exec(f'Phasesv{d}[0]*=0')
    for i in range(modes):
        exec(f'globals()[\'\'.join([\'v\',str(i+1)])][i]=[PHnumbers*diff(Phasesv{d}[i],Phases{d}[i])]')                     
    conteo = d
    for j in range(modes):
        for i in range(modes):
            if j<i:
                conteo = conteo + 1
                exec(f'v{conteo+1}=[[0]]*modes')
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][i]=[PHnumbers2*diff(Phasesv{d}[i],Phases{d}[i])]')  
                exec(f'globals()[\'\'.join([\'v\',str(conteo+1)])][j]=[PHnumbers2*diff(Phasesv{d}[j],Phases{d}[j])]')  
exec(f'Vec{d}=[]'),exec(f'Coeff{d}=[]'), exec(f'Nii{d}=[]'), exec(f'Nij{d}=[]')
for i in range(comb):
        exec(f'Vec{d}.append(globals()[\'\'.join([\'v\',str(i+1)])])')
for i in range(len(Vec4)):
    print(Vec4[i])

The second code show problem: 'theta_1 is not defined'

4
  • Sorry for an unrelated question, but is this converted from some other prorgamming langauge? Commented Sep 8, 2020 at 7:44
  • @Italo Can you review your code again and to get indentation correct please? Commented Sep 8, 2020 at 21:07
  • @VPfB the script is not from of other programming language Commented Sep 9, 2020 at 6:31
  • @NamGVU the identation is not problem, i review the code. Commented Sep 9, 2020 at 6:32

2 Answers 2

2

From the documentation for exec:

Note: The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function exec() returns.

So, in essence, you can't use exec to mess with local variables, for interpreter efficiency reasons. The same restriction does not apply to globals, which is why your first version works.

As for what to do: well, the exec calls are highly unnecessary. Simply execute the Python code normally, without the messy exec calls. It'll honestly probably run faster too. In all of the places where you hack together classes of variables with similar names, just replace them with a single array or dictionary, depending on use case.

Sign up to request clarification or add additional context in comments.

Comments

2

Looks like it might be an indentation error.

Your first sample is

for d in range(1,9):                                                                                                     
    modes=d+1
    if statetype==1: 

while your new definition is

for d in range(1,9):                                                                                                     
    modes=d+1
if statetype==1:    

around line 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.