简体   繁体   English

从另一个函数调用一个函数内部定义的变量

[英]Calling variable defined inside one function from another function

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.我之前定义lists ,所以oneFunction(lists)可以完美运行。

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) .我的问题是在第 6 行调用word 。我试图用相同的word=random.choice(lists[category])定义在第一个函数之外定义word ,但这使得word始终相同,即使我调用oneFunction(lists) .

I want to be able to, every time I call the first function and then the second, have a different word .我希望能够,每次我调用第一个函数,然后是第二个函数时,都有一个不同的word

Can I do this without defining that word outside the oneFunction(lists) ?我可以在不定义oneFunction(lists)之外的word的情况下执行此操作吗?

One approach would be to make oneFunction return the word so that you can use oneFunction instead of word in anotherFunction :一种方法是让oneFunction返回单词,以便您可以在anotherFunction中使用oneFunction而不是word

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 :另一种方法是让anotherFunction接受word作为参数,您可以从调用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:最后,你可以在一个类中定义你的两个函数,并让word成为一个成员:

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()

Everything in python is considered as object so functions are also objects. python中的一切都被视为对象,因此函数也是对象。 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)

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.这样做的好处是你的函数可能在不同的模块中并且需要访问变量。

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)
def oneFunction(lists):
    category=random.choice(list(lists.keys()))
    word=random.choice(lists[category])
    return word

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM