[PYTHON] Implementation of object authenticity judgment condition using __bool__ method

reserve_bool.py



import math

class Coordinate:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __bool__(self):
        print('__bool__')
        return self.x != 0 or self.y != 0
    def __len__(self):
        print('__len__')
        return int(math.sqrt(self.x **2 + self.y **2))

if __name__ == '__main__':
    c = Coordinate(0,0)
    if c:
        print('c is True.')
    else:
        print('c is False.')

Self-study python Quoted from Chapter 11 Object-Oriented Syntax

Recommended Posts

Implementation of object authenticity judgment condition using __bool__ method
Implementation of TF-IDF using gensim
Verification and implementation of video reconstruction method using GRU and Autoencoder
Judgment of backlit image using OpenCV
Einsum implementation of value iterative method
Implementation of desktop notifications using Python
Implementation and experiment of convex clustering method
Implementation of dialogue system using Chainer [seq2seq]
Implementation of "blurred" neural network using Chainer