0

Hi I'm trying to figure out the way to trigger action - stop a program from running IF it's var value got populated.

class Checker:
  def __init__(self, value):
    self.value = value
    self.error = None

    self.check1()  # I'd like to stop run after any of checks IF self.error is populated
    self.check2()  # But I want to do it without explicitly stating 'if self.error: return' after each check.
    self.check3()  # Basically, I'm looking for a trigger that will initiate return to break flow as soon as self.error gets populated.

    self.is_valid = not self.error

  def check1(self):
    if condition:
      self.error = "Found error1"

  def check2(self):
    if condition:
      self.error = "Found error2"

  def check3(self):
    if condition:
      self.error = "Found error3"


note: I don't want to throw an error but rather record it in class attr to be able later to extract it.

venv
  • 117
  • 9
  • See [this](https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change) – AzyCrw4282 Jun 10 '20 at 22:42
  • @AzyCrw4282 yes, I was considering Observer pattern, but I'm not sure how to terminate inside of the init. Is it possible? If not, I can move logic to the __call__ method. – venv Jun 11 '20 at 01:46
  • I am not sure how the termination inside `init` method would work. You are best off with the `call` method imo – AzyCrw4282 Jun 11 '20 at 16:18

0 Answers0