1

I edited my thread based on feedback. Basically, I need to use a couple of variables from function 1, and I need to print it in function 2.

How do I go about doing that?

Hope to hear from you.

Cake.

def function_one():
    number_one = 5
    number_two = 6
    number_three = 7

def function_two():
    print(number_one)
    print(number_two)
    print(number_three)

function_one()
function_two()
Cake
  • 493
  • 7
  • 16

4 Answers4

2

OK, so your variables are caught inside the scope of the function. To use them outside that scope, you need to return them out, e.g.:

def function_one():
  number_one = 5
  number_two = 6
  number_three = 7
  return number_one,number_two, number_three

def function_two(number1, number2, number3):
  print(number1)
  print(number2)
  print(number3)

one, two, three = function_one()
function_two(one, two, three)

and here I have made the various vars different in their naming in their different scopes to make it all more apparent.

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • Thank you so much for replying. Is returning it in an array the most elegant solution? – Cake Sep 11 '15 at 17:29
  • 1
    there are many things to do to make it elegant. I am taking what is hopefully a very simple approach to make things clear :) walk, then run :) – Shawn Mehan Sep 11 '15 at 17:31
  • @Cake That is a tuple, and it is packed and unpacked automatically when you return and assign. – saarrrr Sep 11 '15 at 17:31
2

Shawn's answer is great, very straight-forward and almost certainly what you are looking for. He suggests you bring the variables out of the function_one scope by returning them. Another way to solve the problem is to bring your function_two into function_one's scope with a closure.

def function_one():
  num_one = 5
  num_two = 6
  num_three = 7

  def function_two():
    print(num_one)
    print(num_two)
    print(num_three)

  return function_two

func_two = function_one()
func_two()

Edit to address your comment. You could also call function_two directly like this. But this is less readable and unpythonic IMO.

function_one()()

saarrrr
  • 2,754
  • 1
  • 16
  • 26
  • Perfect, I used your method. I put my function two within my function one scope. However, I'm not sure what you mean with func_two = function_one() – Cake Sep 11 '15 at 17:42
  • @Cake Look closely at the definition of function_one. At the end, it returns function_two, and that function must be assigned to a variable in order to be called later. That makes func_two a function so you add () to it to call it. – saarrrr Sep 11 '15 at 17:46
1

just use the return statement it'll work like charm

def function_one():
    num=5
    return num

def function_two():
    print(function_one())

function_two()
Bhawesh Chandola
  • 511
  • 5
  • 19
0

Option 1: use global variables. - Using global variables in a function other than the one that created them (for examples)

Option 2: return the values

ex.

def func_one():
    var1 = 1
    var2 = 2
    return var1, var2

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

# you can return to multiple vars like:
one, two = func_one()
func_two(one, two)

# this would print 1 and 2

# if you return multiple values to one var, they will return as a list
# returning one value per function may keep your code cleaner
the_vars = func_one()
func_two(the_vars[0], the_vars[1])

# this would print 1 and 2 also
Community
  • 1
  • 1
jdf
  • 699
  • 11
  • 21