0

I am trying to create an abstract base class. When using the syntax of python 3, it work perfectly. But then when I switch to the syntax of python 2.7 it just wont work.

the following code is written in 2.7 syntax, and if I run it, it will work, when it shouldn't, there is no foo method in the StillAbstract class.

from abc import ABCMeta, abstractmethod


class Abstract:
    __metaclass__ = ABCMeta

    @abstractmethod
    def foo(self):
        pass


class StillAbstract(Abstract):
    pass


if __name__ == '__main__':
    a = StillAbstract()

But then when I write it in a 3+ syntax, it works the way it should, failing with an error that the abstract base class gives.

from abc import ABCMeta, abstractmethod


class Abstract(metaclass=ABCMeta):

    @abstractmethod
    def foo(self):
        pass


class StillAbstract(Abstract):
    pass


if __name__ == '__main__':
    a = StillAbstract()

Any idea why it work that way?

Max
  • 907
  • 2
  • 13
  • 27

0 Answers0