Pythonの抽象メソッド [重複] 質問する

Pythonの抽象メソッド [重複] 質問する

Python で継承を使用するのに問題があります。Java では概念が簡単すぎるように思えるのですが、Python では今まで理解できず、少なくとも私にとっては驚きです。

次のようなプロトタイプがあります:

class Shape():
   def __init__(self, shape_name):
       self.shape = shape_name

class Rectangle(Shape):
   def __init__(self, name):
       self.shape = name

上記のコードでは、すべてのサブクラスに実装する必要がある抽象メソッドをどのように作成できますか?

ベストアンサー1

abc が導入される前は、これを頻繁に目にしていました。

class Base(object):
    def go(self):
        raise NotImplementedError("Please Implement this method")


class Specialized(Base):
    def go(self):
        print "Consider me implemented"

おすすめ記事