Python Abstract Attribute

后端 未结 1 838
情话喂你
情话喂你 2021-01-28 06:06

I will only have a single Abstract Class in this particular module and so I\'m trying to avoid importing the \"ABC\" package. See below for my attempt and the issue I\'m running

相关标签:
1条回答
  • 2021-01-28 06:14

    You're expecting each child class to have self.requirements right? So change the following code to this.

    class Event(object):
        @property
        def requirements(self):
            try:
                return self._requirements
            except AttributeError:
                raise NotImplementedError('subclasses must have requirements')
    

    That way it will return self.requirements. If self.requirements hasn't been implemented by the child class it will raise a not implemented error.

    EDIT: Updated return to avoid never-ending loop.

    0 讨论(0)
提交回复
热议问题