关于python 2.7:未引发python3抽象类typeerror

Python3 abstract class TypeError not raised

我在运行python 2.7、3.4和3.5。只有2.7使用以下代码引发类型错误。我想知道我是否做错了什么,这是一个已知的错误还是其他的错误?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from abc import ABCMeta, abstractmethod

class Base(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def bar(self):
        pass


class Concrete(Base):
    pass


confused = Concrete()

在Python2.7中,我得到了以下(有用的)错误:

1
2
3
4
Traceback (most recent call last):
  File"confused.py", line 16, in <module>
    confused = Concrete()
TypeError: Can't instantiate abstract class Concrete with abstract methods bar

但是在python3.x中,它运行时没有错误(错误)。谢谢。


它们在python2.x和python3.x中表现不同。

Python 3.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# importing abstract base classes module
import abc

class GetterSetter(abc.ABC):
    '''
    ABSTRACT BASE CLASSES:

    -  An abstract base class is a kind of 'model' for other classes to be defined.
        - It is not designed to construct instances, but can be subclassed by regular classes

    - Abstract classes can define interface, or methods that must be implemented by its subclasses.

    '''


    # Abstract classes are not designed to be instantiated, only to be subclassed

    #  decorator for abstract class
    @abc.abstractmethod
    def set_val(self, input):
       """set the value in the instance"""
        return

    @abc.abstractmethod
    def get_val(self):
       """Get and return a value from the instance..."""
        return

# Inheriting from the above abstract class
class MyClass(GetterSetter):

    # methods overriding in the GetterSetter
    def set_val(self, input):
        self.val = input

    def get_val(self):
        return self.val


# Instantiate
x = MyClass()
print(x) # prints the instance <__main__.MyClass object at 0x10218ee48>
x = GetterSetter() #throws error, abstract classes can't be instantiated

Python 2.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import abc

class GetterSetter(object):
    # meta class is used to define other classes
    __metaclass__ = abc.ABCMeta


    #  decorator for abstract class
    @abc.abstractmethod
    def set_val(self, input):
       """set the value in the instance"""
        return

    @abc.abstractmethod
    def get_val(self):
       """Get and return a value from the instance..."""
        return

# Inheriting from the above abstract class
class MyClass(GetterSetter):

    # methods overriding in the GetterSetter
    def set_val(self, input):
        self.val = input

    def get_val(self):
        return self.val


# Instantiate
x = GetterSetter()
print(x)
x = GetterSetter() #throws error, abstract classes can't be instantiated

在这里检查我的答案。


声明在python3中更改的抽象基类为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import abc

class Base(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def bar(self):
        pass


class Concrete(Base):
    pass


Concrete() # Will raise a TypeError