I am trying to access a local function variable outside the function in Python. So, for example,
bye = ''
def hi():
global bye
something
something
bye = 5
sigh = 10
hi()
print bye
The above works fine as it should. Since I want to find out if I can access bye outside hi() without using global bye, I tried:
def hi():
something
something
bye = 5
sigh = 10
return
hi()
x = hi()
print x.bye
The above gives AttributeError: 'NoneType' object has no attribute 'bye'.
Then, I tried:
def hi():
something
something
bye = 5
sigh = 10
return bye
hi()
x = hi()
print x.bye
This time it doesn't give even an error.
So, is there a way to access a local function variable (bye) outside its function (hi()) without using globals and without printing out variable sigh as well? (Question was edited to include sigh after @hcwhsa 's comment below.