0

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)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    No you can't, that's what "local" means. Besides that, you don't define a variable of any kind named `my_reply` anywhere in your code. – martineau Sep 25 '16 at 03:01
  • in the sample code , I have a my function , in my function I am assigning reply to global variable . When I print global variable in my function i.e t . Printing value as expected . but I am not sure why in test function its not printing ? because t is updated with reply in my function. – Anil Kumar Thotamalla Sep 25 '16 at 04:56
  • In the function `my()`, you assign a value to a local variable named `reply` and then assign that to a global variable named `t`. In the `test()` function you reference `t`, the global variable, and another named `my_reply`, which has not been defined anywhere. `my_reply` is not another name for the local variable `reply` in the other function. You can't do that. You could create a function attribute in `my()` and access it in `test()` using the technique shown in [my answer](http://stackoverflow.com/a/19327712/355230) to another question. This would allow you to define a `my.reply`. – martineau Sep 25 '16 at 14:38

3 Answers3

1

There are different ways to access the local scope of a function. You can return the whole local scope if you want by calling locals() this will give you the entire local scope of a function, it's atypical to save the local scope. For your functions you can save the variables that you need in the function itself, func.var = value:

def test():
    print("I: ", my.t)
    print("IIIIIIII: ", my.reply)

def my():
    my.reply = foo() 
    my.t = m.reply
    print("reply:", my.reply)
    print("ttttt:", my.t)

You can now access t and reply normally. Each time you call your function my and reply will be updated, whatever foo returns will be assigned to my.reply.

GIZ
  • 4,409
  • 1
  • 24
  • 43
0

As I know, you can't get access to local variable outside function. But even you can in my opinion it will be a bad practice.

Why not use function or class.

connections = 0

def set_connection_number(value):
    global connections; connections = value;

def get_connection_number():
    global connections;
    return connections;

# test
set_connection_number(10)
print("Current connections {}".format(get_connection_number()))
pavnik
  • 466
  • 2
  • 7
0

Except closure you wont have access to local vars outside the scope of your functions. If variables have to be shared across different methods better make them as global as @pavnik has mentioned.

saurabh baid
  • 1,819
  • 1
  • 14
  • 26