Equivalent of NotImplementedError for fields in Python

前端 未结 8 1349
北海茫月
北海茫月 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:42

    Alternate answer:

    @property
    def NotImplementedField(self):
        raise NotImplementedError
    
    class a(object):
        x = NotImplementedField
    
    class b(a):
        # x = 5
        pass
    
    b().x
    a().x
    

    This is like Evan's, but concise and cheap--you'll only get a single instance of NotImplementedField.

提交回复
热议问题