18
15

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 5 years have passed since last update.

PythonのNotImplementedError

Posted at

ドキュメントに

exception NotImplementedError
この例外は RuntimeError から派生しています。ユーザ定義の基底クラスにおいて、抽象メソッドが派生クラスでオーバライドされることを要求する場合、この例外を送出しなくてはなりません。

と書いてあります。とりあえず絶対オバーライドしろよって場合には使えばよさそうです。

# -*- coding: utf-8 -*-

class BaseTest(object):
    def hogehoge(self):
        raise NotImplementedError()


class Test(BaseTest):
    def hogehoge(self):
        print "hogehoge"


class Test2(BaseTest):
    pass


if __name__ == "__main__":
    test = Test()
    test.hogehoge()

    test2 = Test2()
    test2.hogehoge()

出力結果

hogehoge
Traceback (most recent call last):
  File "test.py", line 22, in <module>
    test2.hogehoge()
  File "test.py", line 5, in hogehoge
    raise NotImplementedError()
NotImplementedError
18
15
1

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
18
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?