Equivalent of NotImplementedError for fields in Python

前端 未结 8 1340
北海茫月
北海茫月 2020-12-07 18:56

In Python 2.x when you want to mark a method as abstract, you can define it like so:

class Base:
    def foo(self):
        raise NotImplementedError(\"Subcl         


        
8条回答
  •  轮回少年
    2020-12-07 19:35

    Yes, you can. Use the @property decorator. For instance, if you have a field called "example" then can't you do something like this:

    class Base(object):
    
        @property
        def example(self):
            raise NotImplementedError("Subclasses should implement this!")
    

    Running the following produces a NotImplementedError just like you want.

    b = Base()
    print b.example
    

提交回复
热议问题