You really should avoid using global variables. Regardless, here's how to do it:
class example:
def fun1(self):
# globals sent # Not needed in this special case.
exec("sent = {}", globals())
print('in fun1, "sent" is now', sent )
v = example()
print(v.fun1()) # Result will be None because fun1() doesn't return anything.
print('after call to fun1(), global "sent" is', sent)
Output:
in fun1, "sent" is now {}
None
after call to fun1(), global "sent" is {}
A global declaration only does something inside a function or class method and even then is only needed when the global variable's value is going to be set to something.
However, as a special case, one isn't really needed here in the fun1() method because it explicitly passes globals() (but not a separate locals dict) when it calls exec(). It might be a good idea to put one in anyway to make it more clear what's going on.
Using exec() this way is explained in its documentation which says:
If only globals is provided, it must be a dictionary, which will be used for both the global and the local variables.
(emphasis mine)
Here's a way to avoid referencing a global variable at all in the method:
class example:
def fun1(self):
namespace = {}
exec("sent = {}", namespace)
sent = namespace['sent'] # Retrieve result.
print('in fun1, "sent" is now', sent )
return sent
v = example()
sent = v.fun1()
print('after calling fun1(), "sent" got set to', sent)
Output:
in fun1, "sent" is now {}
after calling fun1(), "sent" got set to {}