0

When needing the output of a function such as:

def _hasWon(self):
   if self == True
      return True
   else 
      return False

Then later setting this via:

self._hasWon(True)

But if I need to check if _hasWon is True later, how would I check it without calling the function? I currently have:

if self._hasWon(self) 

This calls the function again, however, which will change the existing value. How should this be rewritten?

1
  • Can you please explain what are you trying to do? Commented Jul 29, 2020 at 7:06

2 Answers 2

1

You are probably doing something wrong here.

If you are using a class, you could something like

Class MyClass:
    def hasWon(self, won):
        self.won = won # won is bool

Now set the value like this

obj = MyClass()
obj.hasWon(True)

And get the value as

obj.hasWon

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this setup is more logical than what I was doing.
Glad this helped. Please accept my answer if it solved your problem :)
0

You have an option to keep the state in the function, but using a class seems to be more common solution

def _hasWon(self, previous=[]):
    if not previous:
        previous.append(self)
    return next(iter(previous))

It will return the first value got passed until you pass an empty list as a second argument to set a new state

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.