Python中具有特定参数的抽象方法

7

我使用abc模块实现了抽象类。以下程序没有问题。

有没有办法使它失败,因为抽象方法MyMethod有一个参数a,但是Derivative类中'MyMethod'的实现没有?所以我想不仅指定接口类Base中的方法,还要指定这些方法的参数。

import abc

#Abstract class
class Base(object):
    __metaclass__  = abc.ABCMeta

    @abc.abstractmethod
    def MyMethod(self, a):
        'MyMethod prints a'


class Derivative(Base)

    def MyMethod(self):
        print 'MyMethod'

1
不,abc.abstractmethod 不会强制要求必须存在哪些参数。 - Martijn Pieters
Martijn,有没有其他强制参数的方法?也许不同于abc的东西。 - Konstantin
zope.interface 可以验证方法签名。但是您需要显式地执行此操作(例如在单元测试中)。 - Martijn Pieters
3
你的项目是否足够复杂以需要进行这种静态检查?如果是,Python可能不是你最好的语言选择。 - Sven Marnach
1
Sven,我正在实现一个用于网络设备黑盒测试的系统。我使用抽象类来指定我的软件在各种CLI / SNMP的HAL层中的接口(除了硬件适配层外,各种设备的测试看起来都是相同的)。Python非常方便编写这些测试。 - Konstantin
1个回答

3
以下代码是从一个代理类中复制的,它的工作方式类似。它检查所有方法是否存在以及方法签名是否相同。这项工作在_checkImplementation()中完成。请注意以ourf和theirf开头的两行代码;_getMethodDeclaration()将签名转换为字符串。在这里,我选择要求两者完全相同:
  @classmethod
  def _isDelegatableIdentifier(cls, methodName):
    return not (methodName.startswith('_') or methodName.startswith('proxy'))



  @classmethod
  def _getMethods(cls, aClass):
    names  = sorted(dir(aClass), key=str.lower)
    attrs  = [(n, getattr(aClass, n)) for n in names if cls._isDelegatableIdentifier(n)]
    return dict((n, a) for n, a in attrs if inspect.ismethod(a))



  @classmethod
  def _getMethodDeclaration(cls, aMethod):
    try:
      name = aMethod.__name__
      spec = inspect.getargspec(aMethod)
      args = inspect.formatargspec(spec.args, spec.varargs, spec.keywords, spec.defaults)
      return '%s%s' % (name, args)
    except TypeError, e:
      return '%s(cls, ...)' % (name)



  @classmethod    
  def _checkImplementation(cls, aImplementation):
    """
    the implementation must implement at least all methods of this proxy,
    unless the methods is private ('_xxxx()') or it is marked as a proxy-method
    ('proxyXxxxxx()'); also check signature (must be identical).
    @param aImplementation: implementing object
    """
    missing = {}

    ours   = cls._getMethods(cls)
    theirs = cls._getMethods(aImplementation)

    for name, method in ours.iteritems():
        if not (theirs.has_key(name)):
          missing[name + "()"] = "not implemented"
          continue


        ourf   = cls._getMethodDeclaration(method)
        theirf = cls._getMethodDeclaration(theirs[name])

        if not (ourf == theirf):
          missing[name + "()"] = "method signature differs"

    if not (len(missing) == 0):
      raise Exception('incompatible Implementation-implementation %s: %s' % (aImplementation.__class__.__name__, missing))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接