相关疑难解决方法(0)

在Python中继承方法的文档字符串

我有一个带有文档字符串的OO层次结构,它需要与代码本身一样多的维护.例如,

class Swallow(object):
    def airspeed(self):
        """Returns the airspeed (unladen)"""
        raise NotImplementedError

class AfricanSwallow(Swallow):
    def airspeed(self):
        # whatever
Run Code Online (Sandbox Code Playgroud)

现在,问题是AfricanSwallow.airspeed不继承超类方法的docstring.我知道我可以使用模板方法模式保留文档字符串,即

class Swallow(object):
    def airspeed(self):
        """Returns the airspeed (unladen)"""
        return self._ask_arthur()
Run Code Online (Sandbox Code Playgroud)

_ask_arthur在每个子类中实现.但是,我想知道是否还有另一种方法可以继承docstrings,也许还有一些我尚未发现的装饰器?

python oop inheritance docstring template-method-pattern

51
推荐指数
3
解决办法
6159
查看次数

有什么方法可以与 Cython 获得“接口”?

我的应用程序需要有几个cdef从单个基类继承的Cython类,但仍然实现许多接口。这些接口将用于对isinstance()类进行检查以确保它们符合某些接口。

我知道 Cython 不支持多重继承,但是有什么方法可以实现类似接口的行为。这在 Cython 中似乎是一个相当明显的限制,我相信我不是唯一遇到这个问题的人。

python cython

3
推荐指数
1
解决办法
748
查看次数