using exec function in function and defining variable

Viewed 45

When I run this code I don't get a error. I'm trying to calculate l1 and put into variable a.

m=np.linspace(-np.pi,np.pi,1000)
l1="np.sin(m)"
exec(f"a ={l1}")
print(a)

When I run this I get a error.Why?

def g():
    m=np.linspace(-np.pi,np.pi,1000)
    l1="np.sin(m)"
    exec(f"a ={l1}")
    print(a)
g()

Error:name "a" is not defined

1 Answers
Related