KurgerBing
KurgerBing

Reputation: 193

get a return from a function in another class

I want to get a return value, which changes the value due to an triggered event. like so:

class A:
      def __init__(self):
        self.ev_nearby = 0

      def changeValue(self):
         self.ev_nearby = 1
         return self.ev_nearby

      # I want this value in class B
      def getChangedValue(self):
         return self.ev_nearby

      def logic(self):
          while(somethingHappens) {
             self.changeValue()
          }
      # logic is called in another class

class B:
      A = A()
      # I want the value here like
      valueFromA = A.getChangedValue()

The reason of the while is because I need both values dynamically in a server. But until now, my valueFromA in class B remains still 0, triggered or not triggered.

Upvotes: 0

Views: 4070

Answers (1)

Ghorich
Ghorich

Reputation: 428

It seems that .logic() is called on another instance of the object than the one you are trying to retrieve the value from. To retrieve the value from the same instance you can pass it as a variable in the scope of the other class. For more information look into the basics of Object Oriented Programming (in Python)

Below I have written a small example of how this can be achieved:

class A:
  def __init__(self):
    self.ev_nearby = 0

  def changeValue(self):
     self.ev_nearby = 1
     return self.ev_nearby

  # I want this value in class B
  def getChangedValue(self):
     return self.ev_nearby

  def logic(self):
      while(somethingHappens) {
         self.changeValue()
      }
  # logic is called in another class

class B:
  # I want the value here like
  def getValueFromA(self, A):
     valueFromA = A.getChangedValue()
     return valueFromA

class C:
  A = A()
  A.logic()
  B = B()
  B.getValueFromA(A)

Upvotes: 2

Related Questions