如何将变量从函数传递到同一类中的另一个函数

时间:2019-05-25 21:03:18

标签: python python-3.x

我有一个变量,其值在“某些函数”中增加一个,我想将其最后一个值传递给“另一个函数”!知道此变量位于“某些功能”和“某些功能”中间 这两个功能属于同一类 我希望你对我很了解:) 这是我的代码

class myClass:
      counter = 0
      def someFunction():
          #here is some codes
          string = "my string"
          globale counter
          for i in range(0, len(string))
              counter += 1
          return string

      def anotherFunction():
          return counter  

1 个答案:

答案 0 :(得分:0)

有几种方法可以解决此问题:

  1. Multiple managers are loaded of type: InputManager Multiple managers are loaded of type: GraphicsSettings Multiple managers are loaded of type: PhysicsManager Multiple managers are loaded of type: QualitySettings Multiple managers are loaded of type: Physics2DSettings Multiple managers are loaded of type: VFXManager 中将其退回:
someFunction()
  1. 使它成为实例或类变量(我正在显示前者):
class myClass:
      counter = 0
      def someFunction(self, counter):
          #here is some codes
          string = "my string"
          for i in range(0, len(string))
              counter += 1
          return string, counter

      def anotherFunction(self):
          return someFunction()[1]  
  1. 使用class myClass: def __init__(self): self.counter = 0 def someFunction(self): #here is some codes string = "my string" for i in range(0, len(string)) self.counter += 1 return string def anotherFunction(self): return self.counter 关键字创建一个全局变量。不要这样做。