0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

__bool__メソッドを用いたオブジェクトの真偽判定条件の実装

Posted at
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.')

独習python 第11章オブジェクト指向構文 より引用

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?