I am trying to create a function that will create a new object of a class through exec every time called, but I could not access the object.
Here is a piece of example code:
a.py:
class test(object):
def __init__(self):
self.var='vari'
def testfunc():
exec("""aaa=test()
aaa.var='varia'""")
b.py:
import a
a.testfunc()
print(a.aaa.var)
When running b.py, it returns the following:
Traceback (most recent call last): File "C:\Users\lenovo\Desktop\pythontest\b.py", line 3, in print(a.aaa.var) AttributeError: module 'a' has no attribute 'aaa'
Why is this?