import abc
class SetterGetter(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def set_val(self, inp):
return
@abc.abstractmethod
def get_val(self):
return
class TryAbsMethod(SetterGetter):
def get_val(self):
return self.inp
def set_valz(self, inp):
self.inp = inp
a = TryAbsMethod()
print(a)
根据教程,我期待从上面的代码中获取,但是我得到了python的输出,这很奇怪,有人可以解释一下为什么吗?
/usr/bin/python3.4 /home/murtuza/MyCodes/abstract.py
<__main__.TryAbsMethod object at 0x7f93ba8ab048>
Process finished with exit code 0
TypeError: Can't instantiate abstract class TryAbsMethod with abstract method set_val
答案 0 :(得分:1)
您正在使用Python 3.x.在Python 3.x中,元类声明语法已更改。
class SetterGetter(object, metaclass=abc.ABCMeta): # <---
@abc.abstractmethod
def set_val(self, inp):
return
@abc.abstractmethod
def get_val(self):
return
请参阅Customizing class creation - Data model - Python 3.x documentation,PEP3115 - Metaclasses in Python 3