As others have said, it sounds like you're trying to solve something that would be better off done a different way (see XY problem
)
If hi and bye need to share different types of data, you might be better off using a class. Ex:
class MyGreetings(object):
hello = [1, 2, 3]
def hi(self):
print('hello')
def bye(self):
print(self.hello)
You could also do it with globals:
global hello
def hi():
global hello
hello = [1,2,3]
print("hello")
def bye():
print(hello)
or by having hi return a value:
def hi():
hello = [1,2,3]
print("hello")
return hello
def bye():
hello = hi()
print(hello)
or you could have hi put hello on the hi function itself:
def hi():
hello = [1,2,3]
print("hello")
hi.hello = hello
def bye():
hello = hi.hello
print(hello)
Now that said, the sketchy way to accomplish what you're asking would be to pull out the source code of hi(), and execute the body of the function within bye() and then pull out the variable hello:
import inspect
from textwrap import dedent
def hi():
hello = [1,2,3]
print("hello")
def bye():
sourcelines = inspect.getsourcelines(hi)[0]
my_locals = {}
exec(dedent(''.join(sourcelines[1:])), globals(), my_locals)
hello = my_locals['hello']
print(hello)
hellois a name (read variable), local to thehifunction. The best way, from what I can see, would be to assignhelloin the global space (i.e. where you call the functions) and pass such variable as a parameter to the functions. Or, as suggested below, lethireturnhello