1

I have two functions. They are not a part of any class. Just individual functions. function f1 calls f2. What I would like to do is print in f2 the variables declared in f1.

How can I achieve this?

def f1():
    a = 10
    b = 20
    f2()

def f2():
    print(a)
    print(b)
michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Take a look at http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html – spijs Jan 30 '18 at 22:18
  • I think I did not explicitly state a few things in my question. I know I can pass variables to f2() from f1(). I dont want to do that. I am looking for a capability like : `print – Amol Deodhar Jan 30 '18 at 22:45
  • Could you give me a convincing reason why you actually want to achieve this? :) – spijs Jan 30 '18 at 23:13
  • I have a case where f1() is a function that a fellow team member can write. For logging purposes, I will write a f2() function that will be called by f1(). My f2() function will refer back to f1() and save all variables. That way I eliminate the possibility of the user writing the function f1() to miss out on any variable. – Amol Deodhar Jan 30 '18 at 23:27
  • What you are trying now is not a very good practice. What would happen if your function changes... That would require a change of the other function in a lot of cases. By passing a list or array of variables it doesn't matter if it's empty, contains 2 or even more variables – spijs Jan 31 '18 at 06:01
  • It would also make sense to just pass one variable and call the log function twice. – spijs Jan 31 '18 at 06:10

2 Answers2

1
def f1():
    a = 10
    b = 20
    f2(locals())

def f2(dic):
    for key in dic:
        print(key, '=', dic[key])

f1()

locals() returns the the local variables and values in a dictionary. Calling f2 inside f1 captures the locals of f1 and is passed as the argument dic for processing in f2.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Useful function indeed. Keep in mind that this will print all local variables, which in this case is what was asked of course. – spijs Jan 31 '18 at 21:32
0

You could define the variables outside of the function first and then use the global modifier as in the second example. In this simple case however it would probably make more sense to just pass the variables to f2() (example 1).

You could also take a look at Access a function variable outside the function without using `global` if this is what you are looking for?

You generally also don't want to have code that checks what the caller function is as in this example. In any case your code must not be able to reach a state where a and b do not (yet) exist.

example 1

def f1():
    a = 10
    b = 20
    f2(a, b)

def f2(a, b):
    print(a)
    print(b)

As stated in the comments this is not what you were looking for, so that's why you also have this possibility:

example 2

a=5
b=5

def f1():
    global a
    global b
    a = 10
    b = 20
    f2()

def f2():
    print(a)
    print(b)

Calling f1() will print 10 and 20.

spijs
  • 1,489
  • 18
  • 36