Goal: Need to use local variable in another function. Is that possible in Python?
I would like to use the local variable in some other function. Because in my case I need to use a counter to see the number of connections happening and number of connections releasing/ For that I am maintaining a counter. To implement that I have written sample code for count and returning local variable in another function.
How can I print t
& my_reply
in the test()
function?
Code: counter_glob.py
my_test = 0
t = 0
def test():
print("I: ",t)
print("IIIIIIII: ",my_reply)
def my():
global t
reply = foo()
t = reply
print("reply:",reply)
print("ttttt:",t)
def foo():
global my_test
my_test1 = 0
my_test += 1
print my_test1
my_test1 = my_test
my_test += 1
print("my_test:",my_test1)
return my_test1
my()
Result:
> $ python counter_glob.py
0
('my_test:', 1)
('reply:', 1)
('ttttt:', 1)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…