The following code works as expected
name = "Test"
myname = ""
exec('myname ="' + name + '"')
print(myname)
Which shows as result:
Test
Problem
However, if I define the same within a function in a class and execute it I get as result an empty string.
class new(object):
def __init__(self, name):
self.print(name)
def print(self, name):
myname = ""
exec('myname ="' + name + '"')
print(myname)
a = new("My name")
The above is a toy example code of a bigger code.
Question
How to define the function so as to get the same result? The exec function is actually needed in the bigger code.
exec? What does it say about the behavior in local scopes? Hint, look at the last note.execat all? I half suspect you don't need it as much as you think you do.