2016-05-21 5 views
0

Ich führe Python 2.7, 3.4 und 3.5. Nur 2.7 löst einen TypeError mit dem folgenden Code aus. Ich frage mich, ob ich etwas falsch mache, ist das ein bekannter Fehler oder ist es etwas anderes?Python3 abstrakte Klasse TypeError nicht ausgelöst

from abc import ABCMeta, abstractmethod 

class Base(object): 
    __metaclass__ = ABCMeta 

    @abstractmethod 
    def bar(self): 
     pass 


class Concrete(Base): 
    pass 


confused = Concrete() 

In Python 2.7 erhalte ich die folgenden (hilfreich) Fehler:

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 

Aber in Python3.x läuft es ohne einen Fehler (sehr schlecht). Vielen Dank.

Antwort

0

Deklarieren eine abstrakte Basisklasse geändert in python3 zu:

import abc 

class Base(metaclass=abc.ABCMeta): 

    @abc.abstractmethod 
    def bar(self): 
     pass 


class Concrete(Base): 
    pass 


Concrete() # Will raise a TypeError 
0

Sie verhalten sich anders in Python2.x und Python3.x.

Python3.6

# 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 

Python2.x

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 

Überprüfen Sie meine Antwort here.