从另一个函数调用一个函数中定义的变量 [英] Calling variable defined inside one function from another function

查看:51
本文介绍了从另一个函数调用一个函数中定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个:

def oneFunction(lists):类别=随机.选择(列表(列表.keys()))word=random.choice(lists[category])定义另一个函数():单词中的字母:#problem 在这里打印(_",结束=")

我之前已经定义了 lists,所以 oneFunction(lists) 可以完美运行.

我的问题是在第 6 行中调用 word.我试图在第一个函数之外定义 word 与相同的 word=random.choice(lists[category]) 定义,但这使得 word 始终相同,即使我调用 oneFunction(lists).

我希望每次调用第一个函数然后调用第二个函数时都有不同的word.

我可以在不定义 oneFunction(lists) 之外的 word 的情况下做到这一点吗?

解决方案

是的,您应该考虑在一个类中定义两个函数,并使 word 成为成员.这更干净:

类垃圾邮件:def oneFunction(self,lists):类别=随机.选择(列表(列表.keys()))self.word=random.choice(lists[category])def anotherFunction(self):对于 self.word 中的字母:打印(_",结束=")

一旦你创建了一个类,你必须将它实例化为一个对象并访问成员函数

s = Spam()s.oneFunction(列表)s.anotherFunction()

另一种方法是让 oneFunction 返回单词,以便您可以在 anotherFunctiononeFunction 而不是 word代码>

<预><代码>>>>定义一个函数(列表):类别=随机.选择(列表(列表.keys()))返回 random.choice(lists[category])>>>定义另一个函数():对于oneFunction(列表)中的字母:打印(_",结束=")

最后,您还可以制作anotherFunction,接受word 作为参数,您可以从调用oneFunction 的结果中传递该参数><预><代码>>>>def anotherFunction(words):对于文字中的字母:打印(_",结束=")>>>另一个功能(一个功能(列表))

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

解决方案

Yes, you should think of defining both your functions in a Class, and making word a member. This is cleaner :

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 it to an Object and access the member functions

s = Spam()
s.oneFunction(lists)
s.anotherFunction()

Another 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=" ")

And finally, you can also make 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))

这篇关于从另一个函数调用一个函数中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆