3

I have a question that I couldn't find answere also during my testing. Methods pos and neg doesn't work for me.

Let suppose we have a simple class:

class Test:
def __init__(self,
             param=False):
    self.param = param

if __name__ == '__main__':
    c = Test()
    if c:  # which Python magic method is invoked here?
        print("OK")
    else:
        print("Nok")

And now, I want to override magic method which is invoked when I make statement:

if c:

But I don't know which? Please for advice.

rozacek
  • 316
  • 3
  • 6

1 Answers1

7

__bool__() if its defined
see https://docs.python.org/3/library/stdtypes.html

By default an object resolves to True

stacksonstacks
  • 8,613
  • 6
  • 28
  • 44