Calling variable defined inside one function from another function

Question:

if I have this:

def oneFunction(lists):
    category=random.choice(list(lists.keys()))
    word=random.choice(lists[category])

def anotherFunction():
    for letter in word:              #problem is here
        print("_",end=" ")

I have previously defined lists, so oneFunction(lists) works perfectly.

My problem is calling word in line 6. I have tried to define word outside the first function with the same word=random.choice(lists[category]) definition, but that makes word always the same, even if I call oneFunction(lists).

I want to be able to, every time I call the first function and then the second, have a different word.

Can I do this without defining that word outside the oneFunction(lists)?

Asked By: JNat

||

Answers:

One approach would be to make oneFunction return the word so that you can use oneFunction instead of word in anotherFunction :

def oneFunction(lists):
    category = random.choice(list(lists.keys()))
    return random.choice(lists[category])

    
def anotherFunction():
    for letter in oneFunction(lists):              
        print("_", end=" ")

Another approach is making anotherFunction accept word as a parameter which you can pass from the result of calling oneFunction:

def anotherFunction(words):
    for letter in words:              
        print("_", end=" ")
anotherFunction(oneFunction(lists))

And finally, you could define both of your functions in a class, and make word a member:

class Spam:
    def oneFunction(self, lists):
        category=random.choice(list(lists.keys()))
        self.word=random.choice(lists[category])

    def anotherFunction(self):
        for letter in self.word:              
            print("_", end=" ")

Once you make a class, you have to instantiate an instance and access the member functions:

s = Spam()
s.oneFunction(lists)
s.anotherFunction()
Answered By: Abhijit
def anotherFunction(word):
    for letter in word:              
        print("_", end=" ")

def oneFunction(lists):
    category = random.choice(list(lists.keys()))
    word = random.choice(lists[category])
    return anotherFunction(word)
Answered By: Allan Mwesigwa

The simplest option is to use a global variable.
Then create a function that gets the current word.

current_word = ''
def oneFunction(lists):
    global current_word
    word=random.choice(lists[category])
    current_word = word

def anotherFunction():
    for letter in get_word():              
          print("_",end=" ")

 def get_word():
      return current_word

The advantage of this is that maybe your functions are in different modules and need to access the variable.

Answered By: 0n10n_

Everything in python is considered as object so functions are also objects. So you can use this method as well.

def fun1():
    fun1.var = 100
    print(fun1.var)

def fun2():
    print(fun1.var)

fun1()
fun2()

print(fun1.var)
Answered By: Python Learner
def oneFunction(lists):
    category=random.choice(list(lists.keys()))
    word=random.choice(lists[category])
    return word

def anotherFunction():
    for letter in word:             
        print("_",end=" ")
Answered By: Vasanth Desai

so i went ahead and tried to do what came to my head
You could easily make the first function to return the word then use the function in the another function while passing in an the same object in the new function like so:

def oneFunction(lists):
    category=random.choice(list(lists.keys()))
    word=random.choice(lists[category])
    return word

def anotherFunction(sameList):
    for letter in oneFunction(sameList):             
        print(letter)
Answered By: Lateef Taiwo
def abc():
    global x
    x = 4 +5 
    return x

def klm():
    y = x + 5
    print(y)

When we want to use a function variable into another function , we can simply use ‘global’ keyword.
Note: This is just a simple usage, but if you want to use it within a class environment, then you can refer above answers.

Answered By: DANISH REZA

Solution 1:

Either define variable as global, then print the variable value from the second script. Ex:

python_file1.py
x = 20

def test():
  global x
  return x
test()

python_file2.py
from python_file1 import *
print(x)

Solution 2:

This solution can be useful when the variable in function 1 is not predefined. In this case need to store the output of first python file in a new variable in second python file. Ex:

python_file1.py
def add(x,y):
    return x + y


if __name__ =="__main__"
add(x=int(input("Enter x"),y = int(input("Enter y"))

python_file2.py
from python_file1 import * 
# Assign functions of python file 1 in a variable of 2nd python file
add_file2 = add(5,6)

def add2()
  print(add_file2)

add2()
Answered By: Prosenjit Bari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.