【问题标题】:Calling variable defined inside one function from another function从另一个函数调用一个函数内部定义的变量
【发布时间】:2012-04-25 18:15:24
【问题描述】:

如果我有这个:

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

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

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

我希望能够在每次调用第一个函数和第二个函数时拥有不同的word。

我可以在不定义 word 之外的 oneFunction(lists) 的情况下执行此操作吗?

【问题讨论】:

  • 为什么不将word 作为参数传递给anotherFunction?考虑def anotherFunction(word): 并相应地调用它。

标签: python function variables


【解决方案1】:

是的,你应该考虑在一个类中定义你的两个函数,并让 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=" ")

创建类后,您必须将其实例化为对象并访问成员函数

s = Spam()
s.oneFunction(lists)
s.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=" ")

最后,你也可以使anotherFunction,接受word作为参数,你可以从调用oneFunction的结果中传递它

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

【讨论】:

  • 谢谢!在第一种方法中,你能解释一下self 部分的作用吗?它是函数的参数吗?
  • @JNat: self 为您提供对当前对象的引用,就像 this(指针)或 this(变量)在著名的 OOP 语言中所提供的。
  • python 告诉我 self 没有定义...这是为什么呢?我需要先导入一些模块吗?
  • @JNat:你必须创建一个类,就像我在示例中展示的那样。我认为您对 OO 不满意,因此我将更新我的答案以添加更多详细信息。
  • 我确实定义了一个包含两个函数的类,但是当调用第一个函数 (Spam.oneFunction(self,lists)) 时,他告诉我 self 没有定义...
【解决方案2】:

python 中的一切都被视为对象,因此函数也是对象。所以你也可以使用这个方法。

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

def fun2():
    print(fun1.var)

fun1()
fun2()

print(fun1.var)

【讨论】:

    【解决方案3】:
    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)
    

    【讨论】:

    • 您可以将单词变量从第一个函数“oneFunction”传递给另一个函数。但是,我想你必须先把另一个函数放在前面,否则你会得到一个错误,“anotherFunction is not defined”。另外,我注意到,你没有对你在 for 循环中引用的“字母”变量做任何事情
    【解决方案4】:

    最简单的选择是使用全局变量。 然后创建一个获取当前单词的函数。

    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
    

    这样做的好处是,可能你的函数在不同的模块中,需要访问变量。

    【讨论】:

      【解决方案5】:
      def oneFunction(lists):
          category=random.choice(list(lists.keys()))
          word=random.choice(lists[category])
          return word
      
      def anotherFunction():
          for letter in word:             
              print("_",end=" ")
      

      【讨论】:

        【解决方案6】:

        所以我继续尝试做我想到的事情 您可以轻松地使第一个函数 return 单词,然后在 another function 中使用该函数,同时在新函数中传入相同的对象,如下所示:

        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)
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 2016-01-04
        相关资源
        最近更新 更多