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

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

问题描述



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

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

c>列表,所以 oneFunction(列表)完美地工作。



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



我希望能够每次调用第一个函数,然后调用第二个函数,不同的 word



我可以在没有定义字的情况下做到这一点之外> oneFunction(lists)

是的,你应该考虑在一个类中定义你的函数,并且使它成为一个成员。这是更清洁的

  class垃圾邮件:
def oneFunction(self,lists):
category = random。 choice(list(lists.keys()))
self.word = random.choice(lists [category])

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

你必须实例化它到一个对象并访问成员函数。

  s =垃圾邮件()
s .one函数(列表)
s.anotherFunction()

另一种方法是将 oneFunction 返回单词,以便您可以在 anotherFunction oneFunction 而不是单词
$ b

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


>>> def anotherFunction():
用于oneFunction(列表)中的字母:
print(_,end =)

最后,您还可以使 anotherFunction ,接受单词作为参数,您可以从调用 oneFunction

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


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 function 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天全站免登陆